38497

Multiple files with Log4j

Question:

I'm using log4j to log several informations. I'd like to separate these infos into multiple files.

My configuration file is similar to this:

#logger_1 log4j.logger.logger_1=debug, logger_1_File log4j.appender.logger_1_File=org.apache.log4j.RollingFileAppender log4j.appender.logger_1_File.layout=org.apache.log4j.PatternLayout log4j.appender.logger_1_File.layout.ConversionPattern=%d{ABSOLUTE} %m%n log4j.appender.logger_1_File.File=${webinf}/logs/logger1/logger1.log log4j.appender.logger_1_File.MaxFileSize=5000KB log4j.appender.logger_1_File.MaxBackupIndex=200 log4j.appender.logger_1_File.encoding=UTF-8 #logger_2 log4j.logger.logger_2=debug, logger_2_File log4j.appender.logger_2_File=org.apache.log4j.RollingFileAppender log4j.appender.logger_2_File.layout=org.apache.log4j.PatternLayout log4j.appender.logger_2_File.layout.ConversionPattern=%d{ABSOLUTE} %m%n log4j.appender.logger_2_File.File=${webinf}/logs/logger2/logger2.log log4j.appender.logger_2_File.MaxFileSize=5000KB log4j.appender.logger_2_File.MaxBackupIndex=200 log4j.appender.logger_2_File.encoding=UTF-8 ...

I have two questions about it:

<ol><li>Is there any way I could abbreviate this (omit the repeated/boilerplate code)? I have to repeat a lot of line when I use several appenders. </li> <li>Is there any performance problem with it? This config means log4j holds several resources to write into files. Does it cause performance problems? I have 9 appenders like these so I'm worried about it.</li> </ol>

Answer1:

First of all, measure the performance of your app with a single appender, with many appenders, with different log levels. <strong>If</strong> you have a performance issue, you need to solve it, otherwise no need to worry about it.

Also read the "Performance" section at the end of the <a href="http://logging.apache.org/log4j/1.2/manual.html" rel="nofollow">Short introduction to log4j</a>.

Recommend

  • Logback does not write log into the file but console
  • How to avoid multiple log files in slf4j logging?
  • PostSharp Diagnostics Toolkit - Can the message format be changed?
  • NPE with Perf4j and Log4j
  • Multiple files with Log4j
  • Unable to create multiple log files based on the ThreadContext map values using routing appender in
  • How can i load my log4j.xml file which is not in classpath and it present in D drive?
  • unable to set MaxFileSize to value “100MB” in log4j
  • Mule ESB: Are the Log4j Config for Batch in Mule need separate configuration?
  • log4j jdbcappender pass username to database table
  • How to avoid CRLF (Carriage Return and Line Feed) in Logback - CWE 117
  • Delete nodes in xml if they contain certain text using sed
  • Why isn't my log4j.properties file getting used?
  • How to systematically add (decorate) some data to log
  • how to configure log file path to current working target directory in log4j2 xml format
  • Log4J 2 not writing into file
  • Why doesn't this particular regex work in JavaScript?
  • log4Net in Windows Forms App does not write log file
  • Log4net writing custom object to sql database using custom appender?
  • Three Js Object3D Button Group Detect Single Object Click While Mouse Movement Causes Object3D Butto
  • jParallax trouble
  • Symfony2 redirect to https route fails (uses wrong port)
  • Log4j cannot find properties file in a simple application
  • PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea
  • Making header act like google chrome mobile app address bar
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Cannot connect to cassandra from Spark
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j