0

I spent a couple of days to figure out a way to use log4j to log into two different files with YAML configuration file. Is it possible?

I have gone through similar questions with properties file and some blog posts, given below.

I have a configuration file like this:

logging:
  # The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
  level: INFO
  # Logger-specific levels.
  loggers:
    # Sets the level for 'com.appraptorsors.herotemps' to DEBUG.
    com.company.project: DEBUG
  appenders:
    - type: console
      threshold: ALL
    - type: file
      threshold: ALL
      currentLogFilename: /home/assets/logs/_1.log
      archivedLogFilenamePattern: ./logs/archive-%d.log.gz
      archivedFileCount: 5
      # The timezone used to format dates. HINT: USE THE DEFAULT, UTC.
      timeZone: UTC

How can i configure this as explained in this accepted answer here? This is exactly what I'm looking for - two loggers invoked by separate handlers. Another reference is here.

My log4j version is 1.2.17 and it's fine to upgrade it.

Any help is much appreciated.

Community
  • 1
  • 1
pMan
  • 8,808
  • 11
  • 32
  • 35
  • 5
    Hi, i have answered this here: http://stackoverflow.com/questions/37834340/logging-into-different-files-based-on-some-condition/37864589#37864589 , let me know if it's unclear – pandaadb Oct 04 '16 at 08:49
  • @pandaadb - I will sure update you in a couple of hours. Thank you so much! – pMan Oct 04 '16 at 11:57
  • @pandaadb - it works perfectly, thanks! Also, how can I add log pattern to that? I want only "%m%n". I tried some combinations, but fails with 'malformed YAML.' – pMan Oct 04 '16 at 14:09
  • I added an example log pattern. If that doesn't work for you, please update your original question with your new config + the exception you are seeing. – pandaadb Oct 04 '16 at 14:40
  • It works for me perfectly. I tried to add something more which failed. What I added is like: PatternLayout: Pattern: "%m%n" – pMan Oct 04 '16 at 15:16
  • you can look at the link, i added the layout: logFormat: '[%level] %msg%n' – pandaadb Oct 04 '16 at 15:18
  • That works well too! Many thanks! – pMan Oct 05 '16 at 04:27

0 Answers0