core/loggingmanager.lua
changeset 1067 21f41b06f1d2
parent 1046 6fef969ff307
child 1078 24c9ee99d900
equal deleted inserted replaced
1065:3806173670f2 1067:21f41b06f1d2
    17 
    17 
    18 module "loggingmanager"
    18 module "loggingmanager"
    19 
    19 
    20 -- The log config used if none specified in the config file
    20 -- The log config used if none specified in the config file
    21 local default_logging = { { to = "console" } };
    21 local default_logging = { { to = "console" } };
       
    22 local default_file_logging = { { to = "file", levels = { min = "info" } } };
    22 
    23 
    23 -- The actual config loggingmanager is using
    24 -- The actual config loggingmanager is using
    24 local logging_config = config.get("*", "core", "log") or default_logging;
    25 local logging_config = config.get("*", "core", "log") or default_logging;
    25 
    26 
    26 local apply_sink_rules;
    27 local apply_sink_rules;
    27 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
    28 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
    28 local get_levels;
    29 local get_levels;
    29 local logging_levels = { "debug", "info", "warn", "error", "critical" }
    30 local logging_levels = { "debug", "info", "warn", "error", "critical" }
    30 
    31 
       
    32 -- Put a rule into action. Requires that the sink type has already been registered.
       
    33 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
    31 local function add_rule(sink_config)
    34 local function add_rule(sink_config)
    32 	local sink_maker = log_sink_types[sink_config.to];
    35 	local sink_maker = log_sink_types[sink_config.to];
    33 	if sink_maker then
    36 	if sink_maker then
    34 		if sink_config.levels and not sink_config.source then
    37 		if sink_config.levels and not sink_config.source then
    35 			-- Create sink
    38 			-- Create sink
    63 	else
    66 	else
    64 		-- No such sink type
    67 		-- No such sink type
    65 	end
    68 	end
    66 end
    69 end
    67 
    70 
    68 -- Search for all rules using a particular sink type,
    71 -- Search for all rules using a particular sink type, and apply
    69 -- and apply them
    72 -- them. Called automatically when a new sink type is added to
       
    73 -- the log_sink_types table.
    70 function apply_sink_rules(sink_type)
    74 function apply_sink_rules(sink_type)
    71 	if type(logging_config) == "table" then
    75 	if type(logging_config) == "table" then
    72 		for _, sink_config in pairs(logging_config) do
    76 		for _, sink_config in pairs(logging_config) do
    73 			if sink_config.to == sink_type then
    77 			if sink_config.to == sink_type then
    74 				add_rule(sink_config);
    78 				add_rule(sink_config);
    75 			end
    79 			end
    76 		end
    80 		end
    77 	elseif type(logging_config) == "string" and sink_type == "file" then
    81 	elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
    78 		-- User specified simply a filename, and the "file" sink type 
    82 		-- User specified simply a filename, and the "file" sink type 
    79 		-- was just added
    83 		-- was just added
       
    84 		for _, sink_config in pairs(default_file_logging) do
       
    85 			sink_config.filename = logging_config;
       
    86 			add_rule(sink_config);
       
    87 			sink_config.filename = nil;
       
    88 		end
    80 	end
    89 	end
    81 end
    90 end
    82 
    91 
    83 
    92 
    84 
    93