Software /
code /
prosody
Annotate
util/logger.lua @ 13219:22763b30e458 0.12
util.prosodyctl.check: Hint about the 'external_addresses' config option
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 17 Jul 2023 14:56:57 +0200 |
parent | 8556:5800f0c7afd6 |
child | 12394:86d6b069b892 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1020
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4937
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
437
diff
changeset
|
7 -- |
7131
ea3eea291321
util.logger: Silence luacheck warning
Kim Alvefur <zash@zash.se>
parents:
7130
diff
changeset
|
8 -- luacheck: ignore 213/level |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
437
diff
changeset
|
9 |
7130
2b48ce0bfe7d
util.logger: Remove unused locals
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
10 local pairs = pairs; |
8556
5800f0c7afd6
util.logger: Import globals (fixes on Lua 5.2)
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
11 local ipairs = ipairs; |
5800f0c7afd6
util.logger: Import globals (fixes on Lua 5.2)
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
12 local require = require; |
262 | 13 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
14 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8553
diff
changeset
|
15 -- luacheck: std none |
30 | 16 |
4627
b046cafc81a8
loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Matthew Wild <mwild1@gmail.com>
parents:
3556
diff
changeset
|
17 local level_sinks = {}; |
262 | 18 |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
19 local make_logger; |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
20 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
21 local function init(name) |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
22 local log_debug = make_logger(name, "debug"); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
23 local log_info = make_logger(name, "info"); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
24 local log_warn = make_logger(name, "warn"); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
25 local log_error = make_logger(name, "error"); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
26 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
27 return function (level, message, ...) |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
28 if level == "debug" then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
29 return log_debug(message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
30 elseif level == "info" then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
31 return log_info(message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
32 elseif level == "warn" then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
33 return log_warn(message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
34 elseif level == "error" then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
35 return log_error(message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
36 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
37 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
38 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
39 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
40 function make_logger(source_name, level) |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
41 local level_handlers = level_sinks[level]; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
42 if not level_handlers then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
43 level_handlers = {}; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
44 level_sinks[level] = level_handlers; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
45 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
46 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
47 local logger = function (message, ...) |
3554
1770f8aaf04a
util.logger: Remove my precious premature optimisation :(
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
48 for i = 1,#level_handlers do |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
49 level_handlers[i](source_name, level, message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
50 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
51 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
52 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
53 return logger; |
30 | 54 end |
55 | |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
56 local function reset() |
3357
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
57 for level, handler_list in pairs(level_sinks) do |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
58 -- Clear all handlers for this level |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
59 for i = 1, #handler_list do |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
60 handler_list[i] = nil; |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
61 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
62 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
63 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
64 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
65 local function add_level_sink(level, sink_function) |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
66 if not level_sinks[level] then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
67 level_sinks[level] = { sink_function }; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
68 else |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
69 level_sinks[level][#level_sinks[level] + 1 ] = sink_function; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
70 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
71 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
72 |
8553
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
73 local function add_simple_sink(simple_sink_function, levels) |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
74 local format = require "util.format".format; |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
75 local function sink_function(name, level, msg, ...) |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
76 return simple_sink_function(name, level, format(msg, ...)); |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
77 end |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
78 for _, level in ipairs(levels or {"debug", "info", "warn", "error"}) do |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
79 add_level_sink(level, sink_function); |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
80 end |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
81 end |
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
82 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
83 return { |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
84 init = init; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
85 make_logger = make_logger; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
86 reset = reset; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
87 add_level_sink = add_level_sink; |
8553
32438665dc5e
util.logger: New method 'add_simple_sink', logger.add_simple_sink(print) works
Matthew Wild <mwild1@gmail.com>
parents:
7131
diff
changeset
|
88 add_simple_sink = add_simple_sink; |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
89 new = make_logger; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
90 }; |