How to create my own Appender in log4j?

Update: the provided solution is valid for Log4J 1.x . If you’re looking for 2.x versions, take a look at this article: How to create a custom appender in log4j2 You should extend AppenderSkeleton class, that (quoting javadoc) “provides the code for common functionality, such as support for threshold filtering and support for general filters.” … Read more

Configuring Log4j Loggers Programmatically

You can add/remove Appender programmatically to Log4j: ConsoleAppender console = new ConsoleAppender(); //create appender //configure the appender String PATTERN = “%d [%p|%c|%C{1}] %m%n”; console.setLayout(new PatternLayout(PATTERN)); console.setThreshold(Level.FATAL); console.activateOptions(); //add appender to any Logger (here is root) Logger.getRootLogger().addAppender(console); FileAppender fa = new FileAppender(); fa.setName(“FileLogger”); fa.setFile(“mylog.log”); fa.setLayout(new PatternLayout(“%d %-5p [%c{1}] %m%n”)); fa.setThreshold(Level.DEBUG); fa.setAppend(true); fa.activateOptions(); //add appender to any … Read more

log4j redirect stdout to DailyRollingFileAppender

// I set up a ConsoleAppender in Log4J to format Stdout/Stderr log4j.rootLogger=DEBUG, CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[%t] %-5p %c – %m%n // And I call this StdOutErrLog.tieSystemOutAndErrToLog() on startup public class StdOutErrLog { private static final Logger logger = Logger.getLogger(StdOutErrLog.class); public static void tieSystemOutAndErrToLog() { System.setOut(createLoggingProxy(System.out)); System.setErr(createLoggingProxy(System.err)); } public static PrintStream createLoggingProxy(final PrintStream realPrintStream) { return … Read more

Setting a log file name to include current date in Log4j

DailyRollingFileAppender is what you exactly searching for. <appender name=”roll” class=”org.apache.log4j.DailyRollingFileAppender”> <param name=”File” value=”application.log” /> <param name=”DatePattern” value=”.yyyy-MM-dd” /> <layout class=”org.apache.log4j.PatternLayout”> <param name=”ConversionPattern” value=”%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n”/> </layout> </appender>

How to Create a Custom Appender in log4j2?

This works quite differently in log4j2 than in log4j-1.2. In log4j2, you would create a plugin for this. The manual has an explanation with an example for a custom appender here: http://logging.apache.org/log4j/2.x/manual/extending.html#Appenders It may be convenient to extend org.apache.logging.log4j.core.appender.AbstractAppender, but this is not required. When you annotate your custom Appender class with @Plugin(name=”MyCustomAppender”, …., the … Read more

How to initialize log4j properly?

Log4j by default looks for a file called log4j.properties or log4j.xml on the classpath. You can control which file it uses to initialize itself by setting system properties as described here (Look for the “Default Initialization Procedure” section). For example: java -Dlog4j.configuration=customName …. Will cause log4j to look for a file called customName on the … Read more

Creating multiple log files of different content with log4j

This should get you started: log4j.rootLogger=QuietAppender, LoudAppender, TRACE # setup A1 log4j.appender.QuietAppender=org.apache.log4j.RollingFileAppender log4j.appender.QuietAppender.Threshold=INFO log4j.appender.QuietAppender.File=quiet.log … # setup A2 log4j.appender.LoudAppender=org.apache.log4j.RollingFileAppender log4j.appender.LoudAppender.Threshold=DEBUG log4j.appender.LoudAppender.File=loud.log … log4j.logger.com.yourpackage.yourclazz=TRACE

How to stop INFO messages displaying on spark console?

Edit your conf/log4j.properties file and change the following line: log4j.rootCategory=INFO, console to log4j.rootCategory=ERROR, console Another approach would be to : Start spark-shell and type in the following: import org.apache.log4j.Logger import org.apache.log4j.Level Logger.getLogger(“org”).setLevel(Level.OFF) Logger.getLogger(“akka”).setLevel(Level.OFF) You won’t see any logs after that. Other options for Level include: all, debug, error, fatal, info, off, trace, trace_int, warn Details … Read more