<dependency> <groupId>org.ops4j.pax.logging</groupId> <artifactId>pax-logging-log4j1</artifactId> <version>1.11.5</version> </dependency> properties log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n import org.ops4j.pax.logging.LogService; import org.ops4j.pax.logging.PaxLoggingService; import java.util.HashMap; import java.util.Map; public class Application { public static void main(String[] args) { Map<String, Object> config = new HashMap<>(); config.put("org.ops4j.pax.logging.property.file", "log4j.properties"); PaxLoggingService paxLoggingService = new org.ops4j.pax.logging.intern.log4j1.InternalLog4JLoggerFactory(); paxLoggingService.setLogService(new LogService() { @Override public void log(int level, String message, Throwable throwable) { System.out.println("[" + level + "] " + message); if (throwable != null) { throwable.printStackTrace(); } } }); paxLoggingService.updated(config); } } import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyClass { private static final Logger logger = LoggerFactory.getLogger(MyClass.class); public void doSomething() { logger.debug("This is a debug message"); logger.info("This is an info message"); logger.error("This is an error message"); } }


上一篇:
下一篇:
切换中文