GithubHelp home page GithubHelp logo

mariocairone / log4j2-prop-lookup Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 14 KB

A log4j2 lookup plugin to load properties from Yaml and properties files

Java 100.00%
log4j2 log4j2-plugin lookup

log4j2-prop-lookup's Introduction

log4j2-prop-lookup

A log4j2 plugin to enable properties lookup from a configurable list of property files.

The file can be either .yaml or ..properties file. If a file has a .yaml extension the properties will be loaded using the yaml property loader. Any other extensions will use the property file properties loader.

Installation

To enable the plugin, include the following dependency in the pom.xml

<dependency>
    <groupId>com.mariocairone</groupId>
    <artifactId>log4j2-prop-lookup</artifactId>
    <version>1.0.1</version>
</dependency> 

Configuration

The plugin is configured using the following properties:

name values description required
prop.files String A comma separated list of file paths true
prop.dir String The base directory for all the files in the list false

NOTE

the filePaths must be relative to the classpath root.

If the optional prop.dir properties is present its value will be used as prefix for each filePath.

The com.mariocairone.log4j2 must be declared in the log4j2.xml configuration file.


Example

A common scenario is to customize the log4j2 configuration with different properties for different environments. We can have an Http appender sending the logs to a sandbox logstash instance for non production deployments and to a different instance for the production deployment. Or we want to customize our layout with properties that are specific for an environment.

to do that I have to create a properties file for each environment:

sandbox.yaml

environment:
  name: "Sandbox"
logstash:
  url: "http://mysandbox.host:8080"

production.yaml

environment:
  name: "Production"
logstash:
  url: "http://myproduction.host:8080"

log4j.xml

I can then use a system properties to select the properties file based on the environment.

<Configuration packages="com.mariocairone.log4j2">
    <Properties>
     <Property name="prop.files">${sys:mule.env}.yaml</Property>
    </Properties>
  ...
    <Http name="http" url="${prop:logstash.url}" >
        <Property name="Content-Type" value="application/json" />
        <JsonLayout complete="true" properties="true" >
          <KeyValuePair key="environment" value="${prop:environment.name}"/>
        </JsonLayout>
    </Http> 

log4j2-prop-lookup's People

Contributors

dependabot[bot] avatar mariocairone avatar

Stargazers

 avatar

Watchers

 avatar  avatar

log4j2-prop-lookup's Issues

Bug: Only last files is loaded into properties

Due to a bug only the last processed files properties will be made available by the plugin.

private void load() {
  if (configuration != null) {
    if (!loaded) {
      StrLookup resolver = configuration.getStrSubstitutor().getVariableResolver();
      final String dir = resolver.lookup(LOG4J2_DIR_PROPERTY_NAME);
      String files = resolver.lookup(LOG4J2_FILES_PROPERTY_NAME);
				
      LOGGER.debug("Properties File: " + files);
      Arrays.asList(files.split(",")).forEach(fileName -> {
        String filePath = getFilePath(dir,fileName);
        PropertiesLoader loader = PropertiesLoaderFactory.createPropertiesLoader(filePath);
        this.properties = loader.getProperties();
      });
    loaded = true;
     }
}

in the load method, when properties are loaded from another file, then the loaded properties need to be merged with the properties already loaded.

...
this.properties.putAll(loader.getProperties());
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.