Mercurial Hosting > luan
comparison src/luan/modules/logging/LuanLogger.java @ 1332:11b7e11f9ed5
cleaner logging
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Feb 2019 21:50:26 -0700 |
parents | 307e76ccd0d6 |
children | 25746915a241 |
comparison
equal
deleted
inserted
replaced
1331:02fe660e7748 | 1332:11b7e11f9ed5 |
---|---|
1 package luan.modules.logging; | 1 package luan.modules.logging; |
2 | 2 |
3 import org.apache.log4j.Logger; | 3 import org.slf4j.Logger; |
4 import org.apache.log4j.LogManager; | 4 import org.slf4j.LoggerFactory; |
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 luan.Luan; | |
10 import luan.LuanState; | 5 import luan.LuanState; |
11 import luan.LuanException; | 6 import luan.LuanException; |
12 import luan.LuanTable; | |
13 | 7 |
14 | 8 |
15 public final class LuanLogger { | 9 public final class LuanLogger { |
16 private final Logger logger; | 10 private final Logger logger; |
17 | 11 |
18 public LuanLogger(Logger logger) { | 12 public LuanLogger(LuanState luan,String name) { |
19 this.logger = logger; | 13 this.logger = getLogger(luan,name); |
20 } | 14 } |
21 | 15 |
22 public void error(LuanState luan,Object obj) throws LuanException { | 16 public void error(LuanState luan,Object obj) throws LuanException { |
23 logger.error( luan.toString(obj) ); | 17 logger.error( luan.toString(obj) ); |
24 } | 18 } |
34 public void debug(LuanState luan,Object obj) throws LuanException { | 28 public void debug(LuanState luan,Object obj) throws LuanException { |
35 logger.debug( luan.toString(obj) ); | 29 logger.debug( luan.toString(obj) ); |
36 } | 30 } |
37 | 31 |
38 | 32 |
39 private static String KEY = "Logger.Repository"; | 33 private static ThreadLocal<LuanState> tl = new ThreadLocal<LuanState>(); |
40 | 34 |
41 public static LoggerRepository getLoggerRepository(LuanState luan) { | 35 public static Logger getLogger(LuanState luan,String name) { |
42 LoggerRepository lr = (LoggerRepository)luan.registry().get(KEY); | 36 try { |
43 return lr != null ? lr : LogManager.getLoggerRepository(); | 37 luan.require("luan:logging/Logging.luan"); // ensure initialization |
38 } catch(LuanException e) { | |
39 throw new RuntimeException(e); | |
40 } | |
41 tl.set(luan); | |
42 try { | |
43 return LoggerFactory.getLogger(name); | |
44 } finally { | |
45 tl.remove(); | |
46 } | |
44 } | 47 } |
45 | 48 |
46 public static void setLoggerRepository(LuanState luan,LoggerRepository lr) throws LuanException { | 49 public static LuanState luan() { |
47 luan.registry().put(KEY,lr); | 50 return tl.get(); |
48 LuanTable module = (LuanTable)luan.require("luan:logging/Logging.luan"); | |
49 module.call( "init_root" ); | |
50 } | 51 } |
51 | |
52 public static void newLoggerRepository(LuanState luan) throws LuanException { | |
53 LoggerRepository lr = new Hierarchy(new RootLogger(Level.DEBUG)); | |
54 setLoggerRepository(luan,lr); | |
55 } | |
56 | |
57 public static Logger getRootLogger(LuanState luan) { | |
58 return getLoggerRepository(luan).getRootLogger(); | |
59 } | |
60 | |
61 public static Logger getLogger(LuanState luan,String name) { | |
62 return getLoggerRepository(luan).getLogger(name); | |
63 } | |
64 | |
65 } | 52 } |