lua-simplelog  Check-in [3fe2180ca0]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Document the workings.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:3fe2180ca0e721581af31d9167fac2dd81530c95411347d5fee39254c68385dc
User & Date: llmII 2019-07-11 13:52:28
Context
2019-07-11
14:43
Fixing up the README for fossil's brand of MarkDown. check-in: 64435ecf59 user: llmII tags: trunk
13:52
Document the workings. check-in: 3fe2180ca0 user: llmII tags: trunk
12:27
Adding a license, copyright, and bare rockspec. check-in: 8b09c6ccc7 user: llmII tags: trunk
Changes

Changes to README.md.



































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
## Overview

lua-simplelog is a logging library that aims to be performant, simple,
and capable. It exposes a log mannager which in turn is used to open
up log files in the log directory. The loggers keep up with dates and
roll to a new file when the date changes. It provides for multiple
levels out of the box. Each log can use either the default write
settings, or their own. Write settings allow for telling the logger
which level's are enabled for which output.

## Future
- Perhaps allow dynamically adding levels and/or writers?

## Usage
    local log_config = {
      -- this directory must exist!
      dir = '/tmp/simplelog',
      daemonized = true,
      debug_info = true,
      logs = {
        default = {
          levels = {
            write = 'info',
            print = 'info'
          }
        },
        log1 = {
          levels = {
            write = 'info',
            print = 'trace'
          }
        }
      }
    }

    local log_manager = require 'simplelog'
    local uses_default = log_manager:open 'loga'
    local log1 = log_manager:open 'log1'
    -- should not print or write to file
    uses_default:trace 'This is a message'
    -- should print and write to file
    uses_default:error('Error %s', 1000)
    -- should print but not write to file
    log1:trace 'This is a message'
    -- should print and write to file
    log1:error 'This is a message too'
    log_manager:close_all()

## Documentation

### Levels
These are the available log levels in order of importance with highest
importance being listed last.
- trace
- debug
- info
- warn
- error
- fatal

### Log manager
This is what's returned when you import the module.

Once you initialize a manager with a config like
`local manager = (require 'simplelog')(cfg_table)`
the following functions become avaliable:

Functions:
- open(string name) - opens a log file with the name given and creates
  said file under the directory given in the configuration. Returns
  the log file object.
- close(string name) - closes a previously opened log file by name
- closeall() - closes all log files managed by this manager

**Configuration is explained below:**
Excerpt from example:

      dir = '/tmp/simplelog',
      daemonized = true,
      debug_info = true,
      logs = {
        default = {
          levels = {
            write = 'info',
            print = 'info'
          }
        },
        log1 = {
          levels = {
            write = 'info',
            print = 'trace'
          }
        }
      }
    }

The table key `dir` is required and is the directory in which log
files will be created. The `logs.default` is also required. The rest
is optional.

Manager configuration:
- dir : The directory to which log files will be written.
- daemonized : Used to determine if it is neccessary to disable
  stdout/stderr logging.
- debug_info: If enabled, the logger will include source line info in
  the output.

Log Configuration:

Each log can be configured to only write log lines above a certain
level. If a log, specified by name in the config, exists, then that
configuration is used for determining when to write information,
otherwise the default. Currently logs only support writing to files
(`write`), or writing to the screen (`print`). Each log config is
found under the configuration table at `config.logs` and
`config.logs.default` must exist. Each log's configuration follows the
same format as `config.logs.default`. Each log config has a `levels`
table and must have 2 keys present (one for each currently supported
write situation). These keys simply must be set to one of the log
levels defined above and only levels equal or greater in importance in
relation to that log will be logged. Look to the excerpt for a
demonstration. The levels per output can be different.

### Log Object

Each log object has a method for each level defined earlier. Accessing
anything other than those methods is unsupported and may cause issues.
If a level isn't supposed to write out, then the level is aliased
basically to nop.

Changes to simplelog.lua.

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

  logfile_base.__index = logfile_base
  logfile_base.__gc    = logfile_base.close

  local logfile_class = setmetatable(
    {
      __init = function(self, config, name)
        self.conf = config[name] or config.default
        self.name, self.dir = name, config.dir
        self.daemonized, self.debug_info = config.daemonized, config.debug_info

        for level, _ in pairs(log_levels) do
          local files = {}
          local srclinegen = self.debug_info and getsrclineinfo or snop
          self[level] = nop







|







142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

  logfile_base.__index = logfile_base
  logfile_base.__gc    = logfile_base.close

  local logfile_class = setmetatable(
    {
      __init = function(self, config, name)
        self.conf = config.logs[name] or config.logs.default
        self.name, self.dir = name, config.dir
        self.daemonized, self.debug_info = config.daemonized, config.debug_info

        for level, _ in pairs(log_levels) do
          local files = {}
          local srclinegen = self.debug_info and getsrclineinfo or snop
          self[level] = nop