1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.slf4j;
18
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.spi.AbstractLoggerAdapter;
21 import org.apache.logging.log4j.spi.LoggerContext;
22 import org.apache.logging.log4j.util.StackLocatorUtil;
23 import org.slf4j.ILoggerFactory;
24 import org.slf4j.Logger;
25
26
27
28
29 public class Log4jLoggerFactory extends AbstractLoggerAdapter<Logger> implements ILoggerFactory {
30
31 private static final String FQCN = Log4jLoggerFactory.class.getName();
32 private static final String PACKAGE = "org.slf4j";
33 private final Log4jMarkerFactory markerFactory;
34
35 public Log4jLoggerFactory(final Log4jMarkerFactory markerFactory) {
36 this.markerFactory = markerFactory;
37 }
38
39
40 @Override
41 protected Logger newLogger(final String name, final LoggerContext context) {
42 final String key = Logger.ROOT_LOGGER_NAME.equals(name) ? LogManager.ROOT_LOGGER_NAME : name;
43 return new Log4jLogger(markerFactory, context.getLogger(key), name);
44 }
45
46 @Override
47 protected LoggerContext getContext() {
48 final Class<?> anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE);
49 return anchor == null ? LogManager.getContext() : getContext(StackLocatorUtil.getCallerClass(anchor));
50 }
51
52
53 Log4jMarkerFactory getMarkerFactory() {
54 return markerFactory;
55 }
56
57
58 }