comparison src/luan/modules/logging/Log4j.java @ 1332:11b7e11f9ed5

cleaner logging
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 21:50:26 -0700
parents src/luan/modules/logging/LuanLogger.java@307e76ccd0d6
children 25746915a241
comparison
equal deleted inserted replaced
1331:02fe660e7748 1332:11b7e11f9ed5
1 package luan.modules.logging;
2
3 import org.apache.log4j.Logger;
4 import org.apache.log4j.LogManager;
5 import org.apache.log4j.Hierarchy;
6 import org.apache.log4j.Level;
7 import org.apache.log4j.spi.LoggerRepository;
8 import org.apache.log4j.spi.RootLogger;
9 import org.apache.log4j.spi.RepositorySelector;
10 import luan.LuanState;
11 import luan.LuanException;
12 import luan.LuanTable;
13
14
15 public final class Log4j {
16 private static String KEY = "Logger.Repository";
17
18 public static void newLoggerRepository(LuanState luan) throws LuanException {
19 LoggerRepository lr = new Hierarchy(new RootLogger(Level.DEBUG));
20 luan.registry().put(KEY,lr);
21 LuanTable module = (LuanTable)luan.require("luan:logging/Log4j.luan");
22 module.call( "init_root" );
23 }
24
25 private static final LoggerRepository defaultLoggerRepository = LogManager.getLoggerRepository();
26
27 private static LoggerRepository getLoggerRepository(LuanState luan) {
28 LoggerRepository lr = (LoggerRepository)luan.registry().get(KEY);
29 return lr != null ? lr : defaultLoggerRepository;
30 }
31
32 static {
33 LogManager.setRepositorySelector( new RepositorySelector() {
34 public LoggerRepository getLoggerRepository() {
35 LuanState luan = LuanLogger.luan();
36 return luan==null ? defaultLoggerRepository : Log4j.getLoggerRepository(luan);
37 }
38 }, new Object() );
39 }
40
41 public static Logger getRootLogger(LuanState luan) {
42 return getLoggerRepository(luan).getRootLogger();
43 }
44
45 public static Logger getLogger(LuanState luan,String name) {
46 return getLoggerRepository(luan).getLogger(name);
47 }
48 }