Mercurial Hosting > luan
comparison src/luan/modules/logging/Logging.luan @ 1332:11b7e11f9ed5
cleaner logging
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Feb 2019 21:50:26 -0700 |
parents | 307e76ccd0d6 |
children | 8d95711f6615 |
comparison
equal
deleted
inserted
replaced
1331:02fe660e7748 | 1332:11b7e11f9ed5 |
---|---|
1 local Luan = require "luan:Luan.luan" | |
2 local error = Luan.error | |
3 java() | 1 java() |
4 local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout" | |
5 local ConsoleAppender = require "java:org.apache.log4j.ConsoleAppender" | |
6 local Level = require "java:org.apache.log4j.Level" | |
7 local LuanLogger = require "java:luan.modules.logging.LuanLogger" | 2 local LuanLogger = require "java:luan.modules.logging.LuanLogger" |
3 | |
4 local Package = require "luan:Package.luan" | |
5 if Package.load("java:org.apache.log4j.Logger") ~= false then | |
6 require "luan:logging/Log4j.luan" | |
7 end | |
8 | 8 |
9 | 9 |
10 local Logging = {} | 10 local Logging = {} |
11 | 11 |
12 function Logging.init_root() | |
13 local root = LuanLogger.getRootLogger() | |
14 local has_appenders = root.getAllAppenders().hasMoreElements() | |
15 if not has_appenders then | |
16 root.setLevel(Level.INFO) | |
17 local layout = EnhancedPatternLayout.new("%d %-5p %c - %m%n") | |
18 local appender = ConsoleAppender.new(layout,"System.err"); | |
19 root.addAppender(appender); | |
20 end | |
21 end | |
22 Logging.init_root() | |
23 | |
24 local function to_luan_logger(log4j_logger) | |
25 local tbl = {} | |
26 | |
27 local luanLogger = LuanLogger.new(log4j_logger) | |
28 tbl.error = luanLogger.error | |
29 tbl.warn = luanLogger.warn | |
30 tbl.info = luanLogger.info | |
31 tbl.debug = luanLogger.debug | |
32 | |
33 function tbl.get_level() | |
34 local level = log4j_logger.getLevel() | |
35 return level and level.toString() | |
36 end | |
37 | |
38 function tbl.get_effective_level() | |
39 local level = log4j_logger.getEffectiveLevel() | |
40 return level and level.toString() | |
41 end | |
42 | |
43 function tbl.set_level(level) | |
44 log4j_logger.setLevel( Level.toLevel(level) ) | |
45 end | |
46 | |
47 return tbl | |
48 end | |
49 | |
50 function Logging.logger(name) | 12 function Logging.logger(name) |
51 return to_luan_logger( LuanLogger.getLogger(name) ) | 13 local luan_logger = LuanLogger.new(name) |
52 end | 14 return { |
53 | 15 error = luan_logger.error |
54 function Logging.root_logger() | 16 warn = luan_logger.warn |
55 return to_luan_logger( LuanLogger.getRootLogger() ) | 17 info = luan_logger.info |
18 debug = luan_logger.debug | |
19 } | |
56 end | 20 end |
57 | 21 |
58 return Logging | 22 return Logging |