Wrap log4j or create your own logger?

I have an application that should log two different types of messages: application log messages and audit messages. Application log messages follow the lo4j standard Loggerperfectly, but there are several required parameters for the audit log.

I think I need to wrap log4j to add additional required parameters to methods debug(), info()etc., but I hate the idea of โ€‹โ€‹wrapping log4j. Should I:

  • Wrap log4j completely and provide my own class Loggerthat calls log4j logger behind the scenes?
  • Extend the log4j class Loggerand add audit log methods with my required parameters?
  • Make something even more elegant, so I don't wrap the logging library ...
+3
source share
4 answers

"message" methods error, warnetc. for log4j Logger, an arbitrary object; it should not be a string. You can create your own message class to contain various options. Loggers can add data in different ways, using a custom class Layoutto add an audit trail.

+4
source

I think you can use log4j for application logs by creating logs like:

private final static Logger log = new Logger(MyClass.class);

And for your audit log, create a specific category:

private final static Logger log = new Logger("AuditTrail");

, , , , .

, .

+4

: , ...

( hellojava LOG4J: )

Log4j, :

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

public class AuditLayout extends PatternLayout {
    // Audit Summary Map:
    // <Level, Counter>

    private static Map<Level, Integer> auditMap = new HashMap<Level, Integer>();

    public static Map<Level, Integer> getAuditMap() {
        return auditMap;
    }

    public static void setAuditMap(Map<Level, Integer> auditMap) {
        AuditLayout.auditMap = auditMap;
    }

    @Override
    public String format(LoggingEvent event) {

        if (event.getMessage() != null && event.getMessage() instanceof String) {
            // Check the message level and update the audit object accordingly:
            if (!auditMap.containsKey(event.getLevel())) {
                auditMap.put(event.getLevel(), 1); 
            } else {
                int i = auditMap.get(event.getLevel());    
                i++;
                auditMap.put(event.getLevel(), i); 
            }
        }
        return super.format(event);
    }
}

, ; .. DEBUG > 2, INFO > 10 ..

log4j.properties( ):

# CONSOLE:
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
# BEFORE::: log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
# AFTER:::
log4j.appender.CONSOLE.layout=com.ca.utils.AuditLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss} %-5p %c line: %L - %m%n

AuditMap Logger:

private static AuditLayout auditLayout = null;
...
if (log.getRootLogger().getAppender("CONSOLE").getLayout() instanceof AuditLayout)
    auditLayout = (AuditLayout) log.getRootLogger().getAppender("CONSOLE").getLayout();
...
System.out.println("auditLayout: " + auditLayout.getAuditMap().toString());

, !

+1

, api NDC/MDC log4j. , .

0

All Articles