changeset 1199:3995cbe5b00a

simplify logging
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 28 Feb 2018 23:21:03 -0700
parents 9d3835e88204
children 1e351b0d385e
files src/luan/host/Init.luan src/luan/modules/logging/Logging.luan
diffstat 2 files changed, 33 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
diff -r 9d3835e88204 -r 3995cbe5b00a src/luan/host/Init.luan
--- a/src/luan/host/Init.luan	Tue Feb 27 21:28:24 2018 -0700
+++ b/src/luan/host/Init.luan	Wed Feb 28 23:21:03 2018 -0700
@@ -28,44 +28,34 @@
 
 java()
 local Logger = require "java:org.apache.log4j.Logger"
-local Logging = require "luan:logging/Logging.luan"
+local Level = require "java:org.apache.log4j.Level"
+local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout"
+local RollingFileAppender = require "java:org.apache.log4j.RollingFileAppender"
 
 local root = gsub(domain,"\.",":")
 
-Logging.layout = "%d %-5p %c{-1} - %m%n"
-Logging.file = dir.."/site/private/local/logs/luan.log"
-Logging.max_file_size = "1MB"
-local one_mb = 1048576
-
-local log_dir = dir.."/site/private/local/logs/"
-Logging.appenders = {
-	[log_dir.."error.log"] = "ERROR"
-	[log_dir.."warn.log"] = "WARN"
-	[log_dir.."info.log"] = "INFO"
-}
+local logger = Logger.getLogger(root)
+local has_appenders = logger.getAllAppenders().hasMoreElements()
+if not has_appenders then
+	logger.setAdditivity(false)
+	local layout = EnhancedPatternLayout.new("%d %-5p %c{-1} - %m%n")
+	local log_dir = dir.."/site/private/local/logs/"
 
-Logging.log4j_root_logger = Logger.getLogger(root)
-Logging.log4j_root_logger.setAdditivity(false)
-
-local old_log_to_file = Logging.log_to_file
+	local function add_appender(file,level)
+		local appender = RollingFileAppender.new(layout, log_dir..file)
+		appender.setMaxFileSize("1MB")
+		logger.addAppender(appender)
+		if level ~= logger.getEffectiveLevel() then
+			appender.setThreshold(level)
+		end
+	end
 
-function Logging.log_to_file(file,logger_name)
-	Io.schemes.file(file)  -- security check
-	logger_name = logger_name and root .. "." .. logger_name
-	local appender = old_log_to_file(file,logger_name)
-	appender.getMaximumFileSize() <= one_mb or error "Logging.max_file_size is too big"
-	return appender
+	add_appender("error.log",Level.ERROR)
+	add_appender("warn.log",Level.WARN)
+	add_appender("info.log",Level.INFO)
 end
 
-local old_init = Logging.init
-
-function Logging.init()
-	Logging.appenders["System.err"] = nil
-	Logging.appenders["System.out"] = nil
-	old_init()
-end
-
-Logging.init()
+local Logging = require "luan:logging/Logging.luan"
 
 local old_logger = Logging.logger
 
diff -r 9d3835e88204 -r 3995cbe5b00a src/luan/modules/logging/Logging.luan
--- a/src/luan/modules/logging/Logging.luan	Tue Feb 27 21:28:24 2018 -0700
+++ b/src/luan/modules/logging/Logging.luan	Wed Feb 28 23:21:03 2018 -0700
@@ -1,69 +1,28 @@
 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"
 local ConsoleAppender = require "java:org.apache.log4j.ConsoleAppender"
 local Level = require "java:org.apache.log4j.Level"
-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.appenders = {
-	["System.err"] = "INFO"
-}
-
-Logging.max_file_size = nil  -- by default is "10MB"
-
-
-Logging.log4j_root_logger = Logger.getRootLogger()
-
-local function to_level(level)
-	return level and Level.toLevel(level)
-end
-
-function Logging.log_to_file(file,logger_name)  -- logger_name is optional, defaults to root logger
-	local appender = RollingFileAppender.new(Logging.ptn_layout, file)
-	appender.setMaxFileSize(Logging.max_file_size)
-	local logger = logger_name and Logger.getLogger(logger_name) or Logging.log4j_root_logger
-	logger.addAppender(appender)
-	return appender
+function Logging.init()
+	local root = Logger.getRootLogger()
+	local has_appenders = root.getAllAppenders().hasMoreElements()
+	if has_appenders then
+		return false
+	end
+	root.setLevel(Level.INFO)
+	local layout = EnhancedPatternLayout.new("%d %-5p %c - %m%n")
+	local appender = ConsoleAppender.new(layout,"System.err");
+	root.addAppender(appender);
+	return true
 end
 
-function Logging.init()
-	Logging.ptn_layout = EnhancedPatternLayout.new(Logging.layout)
-	Logging.log4j_root_logger.removeAllAppenders()
-
-	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)
-
-	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
-
-
 local function to_luan_logger(log4j_logger)
 	local tbl = {}
 
@@ -84,7 +43,7 @@
 	end
 
 	function tbl.set_level(level)
-		log4j_logger.setLevel( to_level(level) )
+		log4j_logger.setLevel( Level.toLevel(level) )
 	end
 
 	return tbl