Log4j
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1, A2
# A1 is set to be a ConsoleAppender
# Using a Patternlayout + Given Pattern below
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# A2 is set to be a NoSQLAppender
# Specifically MongoDB, writing to our DB directly
log4j.appender.A2=org.apache.log4j.appender.nosql.NoSqlAppender
A2.name=MongoDB
A2.ignoreExceptions=true
A2.NoSqlProvider=org.apache.log4j.mongodb.MongoDbProvider
A2.server=localhost
A2.protocol=http
A2.port=27017
A2.databaseName=appLog
A2.collectionName=logtest
Patterns:
Placeholder | Represents | Example |
---|---|---|
%d | Timestamp | %d → 2018-05-15 11:50:15,699 |
%c | Class Name | %c → MyClassWithLogger |
%t | Thread Name | [%t] → [main] |
%m | Message | %m → My super important log message |
%n | A Line Break | %n → \n |
Here are a few complete patter examples ready to use:
# Pattern:
%-4r [%t] %-5p %c - %m%n
# Output:
859 [main] INFO Broker [] - New Listener is added to Broker.
---
# Pattern:
%d %-5p [%t] %C{2} (%F:%L) - %m%n
# Output:
2018-05-15 11:50:15,699 INFO [main] Broker (Broker.java:23) - New Listener is added to Broker.
---
# Pattern:
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
17:13:01.540 [main] ERROR MyApp - Didn't do it.