changeset 1101:14ffce5bb894

make logging more flexible
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 02 Jun 2017 16:42:15 -0600
parents ad6b3b9fef40
children b1c3baf3aa46
files src/luan/modules/logging/Logging.luan
diffstat 1 files changed, 29 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/logging/Logging.luan	Sun May 21 16:12:22 2017 -0600
+++ b/src/luan/modules/logging/Logging.luan	Fri Jun 02 16:42:15 2017 -0600
@@ -1,3 +1,6 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local pairs = Luan.pairs or error()
 java()
 local Logger = require "java:org.apache.log4j.Logger"
 local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout"
@@ -6,15 +9,14 @@
 local RollingFileAppender = require "java:org.apache.log4j.RollingFileAppender"
 local LuanLogger = require "java:luan.modules.logging.LuanLogger"
 
+
 local Logging = {}
 
 Logging.layout = "%d %-5p %c - %m%n"
 
-Logging.level = "INFO"
-
-Logging.console = "System.err"  -- or "System.out" or set to nil for no console
-
-Logging.file = nil  -- set to file name if you want logging to a file
+Logging.appenders = {
+	["System.err"] = "INFO"
+}
 
 Logging.max_file_size = nil  -- by default is "10MB"
 
@@ -34,21 +36,30 @@
 end
 
 function Logging.init()
+	Logging.ptn_layout = EnhancedPatternLayout.new(Logging.layout)
 	Logging.log4j_root_logger.removeAllAppenders()
-	Logging.log4j_root_logger.setLevel( to_level(Logging.level) )
-	Logging.ptn_layout = EnhancedPatternLayout.new(Logging.layout)
+
+	local root_level = Level.FATAL
+	for _, level in pairs(Logging.appenders) do
+		level = to_level(level)
+		if not level.isGreaterOrEqual(root_level) then
+			root_level = level
+		end
+	end
+	Logging.log4j_root_logger.setLevel(root_level)
 
-	if Logging.console ~= nil then
-		Logging.console_appender = ConsoleAppender.new(Logging.ptn_layout,Logging.console)
-		Logging.log4j_root_logger.addAppender(Logging.console_appender)
-	else
-		Logging.console_appender = nil
-	end
-
-	if Logging.file ~= nil then
-		Logging.file_appender = Logging.log_to_file(Logging.file)
-	else
-		Logging.file_appender = nil
+	for name, level in pairs(Logging.appenders) do
+		level = to_level(level)
+		local appender
+		if name == "System.err" or name == "System.out" then
+			appender = ConsoleAppender.new(Logging.ptn_layout,name)
+			Logging.log4j_root_logger.addAppender(appender)
+		else
+			appender = Logging.log_to_file(name)
+		end
+		if level ~= root_level then
+			appender.setThreshold(level)
+		end
 	end
 end