0

I have multiple environments (dev/qa/prod) for my application. I would therefore like to differentiate the log conversion pattern based on environment. I have an env variable set which stores which environment the application is running it. But, how do I get log4j.properties to read this env variable?

This is my what my current properties file looks like:

log4j.rootLogger = INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= [%d{yyyy-MM-dd HH:mm:ss}] my-api.%-5p: %m%n

I have tried following the log4j lookup docs, but this still does not include the environment in my log file.

log4j.appender.stdout.layout.ConversionPattern= [%d{yyyy-MM-dd 
 HH:mm:ss}] ${env:ENVIRONMENT}-my-api.%-5p: %m%n

The output looks like this:

[2018-01-22 14:17:20] -my-api.INFO : some-message.

But I want it to look like this:

[2018-01-22 14:17:20] dev-my-api.INFO : some-message.
Freid001
  • 2,580
  • 3
  • 29
  • 60

1 Answers1

3

You may also try a pattern that has become some sort of standard in Luminus and other frameworks. You create an env directory that holds prod/dev/test subfolders with some additional code and resources. In your lein project, for each profile you specify where to find those files in addition to the default path.

As the result, you've got three different log settings. Each of them will be loaded depending on what are you doing. When just develop the code -- from env/dev/resources/log4j.properties and when running tests -- from env/test/resources/log4j.properties.

Here is an example:

$ tree env
.
├── dev
│   └── resources
│       └── log4j.properties
├── prod
│   └── resources
│       └── log4j.properties
└── test
    └── resources
        └── log4j.properties

Some bits from the project.clj:

  :profiles {:dev {:plugins [[autodoc/lein-autodoc "1.1.1"]]
                   :dependencies [[org.clojure/clojure "1.8.0"]
                                  [log4j/log4j "1.2.17"]]
                   :resource-paths ["env/dev/resources"]}}

For test profile, you probably may want to specify both dev and test paths.

Ivan Grishaev
  • 1,583
  • 10
  • 15