core/loggingmanager.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 03 Aug 2010 10:59:47 +0100
changeset 3438 9a8b716fda14
parent 3358 b5a812cf698c
child 3514 fdae08713c67
permissions -rw-r--r--
loggingmanager: Remove (redundant!) dependency on eventmanager
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2922
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2922
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     4
-- 
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     6
-- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     7
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     8
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local format, rep = string.format, string.rep;
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local pcall = pcall;
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local debug = debug;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    13
local tostring, setmetatable, rawset, pairs, ipairs, type = 
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    14
	tostring, setmetatable, rawset, pairs, ipairs, type;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    15
local io_open, io_write = io.open, io.write;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    16
local math_max, rep = math.max, string.rep;
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
    17
local os_date, os_getenv = os.date, os.getenv;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    18
local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    19
2139
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    20
if os.getenv("__FLUSH_LOG") then
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    21
	local io_flush = io.flush;
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    22
	local _io_write = io_write;
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    23
	io_write = function(...) _io_write(...); io_flush(); end
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    24
end
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    25
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    26
local config = require "core.configmanager";
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
local logger = require "util.logger";
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    28
local prosody = prosody;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    29
1343
a0bee511d144 loggingmanager: Enable debug level for default file logging when 'debug' mode is enabled in the config
Matthew Wild <mwild1@gmail.com>
parents: 1117
diff changeset
    30
local debug_mode = config.get("*", "core", "debug");
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
1046
6fef969ff307 core.loggingmanager: Reinstating global log() function
Matthew Wild <mwild1@gmail.com>
parents: 1031
diff changeset
    32
_G.log = logger.init("general");
6fef969ff307 core.loggingmanager: Reinstating global log() function
Matthew Wild <mwild1@gmail.com>
parents: 1031
diff changeset
    33
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    34
module "loggingmanager"
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    35
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    36
-- The log config used if none specified in the config file (see reload_logging for initialization)
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    37
local default_logging;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    38
local default_file_logging;
2922
0ea2ed371fb2 loggingmanager: Don't use non-standard format specifier to format the timestamp.
Waqas Hussain <waqas20@gmail.com>
parents: 2139
diff changeset
    39
local default_timestamp = "%b %d %H:%M:%S";
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    40
-- The actual config loggingmanager is using
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    41
local logging_config;
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    43
local apply_sink_rules;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    44
local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    45
local get_levels;
1024
1bcc8ca57a7c core.loggingmanager: Add default logging settings (to console) and fill out code for adding sinks which catch all sources
Matthew Wild <mwild1@gmail.com>
parents: 1021
diff changeset
    46
local logging_levels = { "debug", "info", "warn", "error", "critical" }
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
1067
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    48
-- Put a rule into action. Requires that the sink type has already been registered.
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    49
-- This function is called automatically when a new sink type is added [see apply_sink_rules()]
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    50
local function add_rule(sink_config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    51
	local sink_maker = log_sink_types[sink_config.to];
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    52
	if sink_maker then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    53
		if sink_config.levels and not sink_config.source then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    54
			-- Create sink
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    55
			local sink = sink_maker(sink_config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    56
			
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    57
			-- Set sink for all chosen levels
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    58
			for level in pairs(get_levels(sink_config.levels)) do
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    59
				logger.add_level_sink(level, sink);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    60
			end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    61
		elseif sink_config.source and not sink_config.levels then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    62
			logger.add_name_sink(sink_config.source, sink_maker(sink_config));
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    63
		elseif sink_config.source and sink_config.levels then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    64
			local levels = get_levels(sink_config.levels);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    65
			local sink = sink_maker(sink_config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    66
			logger.add_name_sink(sink_config.source,
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    67
				function (name, level, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    68
					if levels[level] then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    69
						return sink(name, level, ...);
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    70
					end
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    71
				end);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    72
		else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    73
			-- All sources
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    74
			-- Create sink
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    75
			local sink = sink_maker(sink_config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    76
			
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    77
			-- Set sink for all levels
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    78
			for _, level in pairs(logging_levels) do
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    79
				logger.add_level_sink(level, sink);
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    80
			end
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
		end
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    82
	else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    83
		-- No such sink type
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	end
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
end
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
1067
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    87
-- Search for all rules using a particular sink type, and apply
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    88
-- them. Called automatically when a new sink type is added to
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    89
-- the log_sink_types table.
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    90
function apply_sink_rules(sink_type)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    91
	if type(logging_config) == "table" then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    92
		for _, sink_config in pairs(logging_config) do
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    93
			if sink_config.to == sink_type then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    94
				add_rule(sink_config);
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    95
			end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    96
		end
1067
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    97
	elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
    98
		-- User specified simply a filename, and the "file" sink type
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    99
		-- was just added
1067
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
   100
		for _, sink_config in pairs(default_file_logging) do
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
   101
			sink_config.filename = logging_config;
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
   102
			add_rule(sink_config);
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
   103
			sink_config.filename = nil;
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
   104
		end
1101
fb096ca4b296 loggingmanager: Support for specifying a single sink with *sinkname (*syslog should now work)
Matthew Wild <mwild1@gmail.com>
parents: 1080
diff changeset
   105
	elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
fb096ca4b296 loggingmanager: Support for specifying a single sink with *sinkname (*syslog should now work)
Matthew Wild <mwild1@gmail.com>
parents: 1080
diff changeset
   106
		-- Log all levels (debug+) to this sink
fb096ca4b296 loggingmanager: Support for specifying a single sink with *sinkname (*syslog should now work)
Matthew Wild <mwild1@gmail.com>
parents: 1080
diff changeset
   107
		add_rule({ levels = { min = "debug" }, to = sink_type });
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   108
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   109
end
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   111
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   112
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   113
--- Helper function to get a set of levels given a "criteria" table
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   114
function get_levels(criteria, set)
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   115
	set = set or {};
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   116
	if type(criteria) == "string" then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   117
		set[criteria] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   118
		return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   119
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   120
	local min, max = criteria.min, criteria.max;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   121
	if min or max then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   122
		local in_range;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   123
		for _, level in ipairs(logging_levels) do
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   124
			if min == level then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   125
				set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   126
				in_range = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   127
			elseif max == level then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   128
				set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   129
				return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   130
			elseif in_range then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   131
				set[level] = true;
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   132
			end
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   133
		end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   134
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   135
	
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   136
	for _, level in ipairs(criteria) do
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   137
		set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   138
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   139
	return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   140
end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   141
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   142
-- Initialize config, etc. --
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   143
function reload_logging()
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   144
	local old_sink_types = {};
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   145
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   146
	for name, sink_maker in pairs(log_sink_types) do
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   147
		old_sink_types[name] = sink_maker;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   148
		log_sink_types[name] = nil;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   149
	end
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   150
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   151
	logger.reset();
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   152
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   153
	default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   154
	default_file_logging = { { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true } };
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   155
	default_timestamp = "%b %d %H:%M:%S";
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   156
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   157
	logging_config = config.get("*", "core", "log") or default_logging;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   158
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   159
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   160
	for name, sink_maker in pairs(old_sink_types) do
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   161
		log_sink_types[name] = sink_maker;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   162
	end
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   163
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   164
	prosody.events.fire_event("logging-reloaded");
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   165
end
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   166
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   167
reload_logging();
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   168
prosody.events.add_handler("config-reloaded", reload_logging);
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   169
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   170
--- Definition of built-in logging sinks ---
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   171
1080
b02290fd8a75 loggingmanager: Add a comment about 'nowhere' sink type
Matthew Wild <mwild1@gmail.com>
parents: 1078
diff changeset
   172
-- Null sink, must enter log_sink_types *first*
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   173
function log_sink_types.nowhere()
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   174
	return function () return false; end;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   175
end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   176
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   177
-- Column width for "source" (used by stdout and console)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   178
local sourcewidth = 20;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   179
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   180
function log_sink_types.stdout()
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   181
	local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   182
	
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   183
	if timestamps == true then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   184
		timestamps = default_timestamp; -- Default format
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   185
	end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   186
	
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   187
	return function (name, level, message, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   188
		sourcewidth = math_max(#name+2, sourcewidth);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   189
		local namelen = #name;
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   190
		if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   191
			io_write(os_date(timestamps), " ");
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   192
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   193
		if ... then
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   194
			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   195
		else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   196
			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   197
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   198
	end
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   199
end
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   200
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   201
do
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   202
	local do_pretty_printing = not os_getenv("WINDIR");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   203
	
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   204
	local logstyles = {};
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   205
	if do_pretty_printing then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   206
		logstyles["info"] = getstyle("bold");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   207
		logstyles["warn"] = getstyle("bold", "yellow");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   208
		logstyles["error"] = getstyle("bold", "red");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   209
	end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   210
	function log_sink_types.console(config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   211
		-- Really if we don't want pretty colours then just use plain stdout
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   212
		if not do_pretty_printing then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   213
			return log_sink_types.stdout(config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   214
		end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   215
		
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   216
		local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   217
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   218
		if timestamps == true then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   219
			timestamps = default_timestamp; -- Default format
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   220
		end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   221
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   222
		return function (name, level, message, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   223
			sourcewidth = math_max(#name+2, sourcewidth);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   224
			local namelen = #name;
1892
adc0c80413ee loggingmanager: Whitespace fix
Matthew Wild <mwild1@gmail.com>
parents: 1613
diff changeset
   225
			
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   226
			if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   227
				io_write(os_date(timestamps), " ");
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   228
			end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   229
			if ... then
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   230
				io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   231
			else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   232
				io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   233
			end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   234
		end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   235
	end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   236
end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   237
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   238
local empty_function = function () end;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   239
function log_sink_types.file(config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   240
	local log = config.filename;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   241
	local logfile = io_open(log, "a+");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   242
	if not logfile then
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   243
		return empty_function;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   244
	end
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   245
	local write, flush = logfile.write, logfile.flush;
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   246
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   247
	prosody.events.add_handler("logging-reloading", function ()
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   248
			if logfile then
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   249
				logfile:close();
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   250
			end
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   251
		end);
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   252
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   253
	local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   254
1613
ebf0813a81f6 core.loggingmanager: Enable timestamps by default for file log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   255
	if timestamps == nil or timestamps == true then
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   256
		timestamps = default_timestamp; -- Default format
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   257
	end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   258
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   259
	return function (name, level, message, ...)
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   260
		if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   261
			write(logfile, os_date(timestamps), " ");
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   262
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   263
		if ... then
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   264
			write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   265
		else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   266
			write(logfile, name, "\t" , level, "\t", message, "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   267
		end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   268
		flush(logfile);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   269
	end;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   270
end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   271
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   272
function register_sink_type(name, sink_maker)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   273
	local old_sink_maker = log_sink_types[name];
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   274
	log_sink_types[name] = sink_maker;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   275
	return old_sink_maker;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   276
end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   277
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   278
return _M;