|
1 | 1 | # LogCompose |
| 2 | + |
| 3 | +[](https://travis-ci.org/tanmaykm/LogCompose.jl) |
| 4 | +[](https://coveralls.io/github/tanmaykm/LogCompose.jl?branch=master) |
| 5 | + |
| 6 | +Provides a way to specify hierarchical logging configuration in a file. |
| 7 | + |
| 8 | +Configuration file is in the form of a TOML file. Configuration sections are named, |
| 9 | +with each section specifying a logger type and parameters needed for its construction. |
| 10 | +Sections inherit parameter values from preceeding sectiona and can override them as well. |
| 11 | +Loggers can be constructed by providing the name of a section. |
| 12 | + |
| 13 | +### Example configuration |
| 14 | + |
| 15 | +```toml |
| 16 | +[file] |
| 17 | +type = "LogRoller.RollingLogger" |
| 18 | +min_level = "Info" |
| 19 | +nfiles = 5 |
| 20 | + |
| 21 | +[syslog] |
| 22 | +type = "SyslogLogging.SyslogLogger" |
| 23 | +facility = "user" |
| 24 | + |
| 25 | +[file.testapp1] |
| 26 | +filename = "/tmp/testapp1.log" |
| 27 | + |
| 28 | +[file.testapp2] |
| 29 | +filename = "/tmp/testapp2.log" |
| 30 | +min_level = "Debug" # overrides min_level to Debug for testapp2 |
| 31 | +nfiles = 10 # overrides nfiles to 10 for testapp2 |
| 32 | + |
| 33 | +[syslog.testapp1] |
| 34 | +identity = "testapp1" |
| 35 | +facility = "daemon" # facility set to daemon instead of default user |
| 36 | + |
| 37 | +[syslog.testapp2] |
| 38 | +identity = "testapp2" |
| 39 | + |
| 40 | +[testapp1] |
| 41 | +type = "LoggingExtras.TeeLogger" |
| 42 | +destinations = ["file.testapp1", "syslog.testapp1"] |
| 43 | + |
| 44 | +[testapp2] |
| 45 | +type = "LoggingExtras.TeeLogger" |
| 46 | +destinations = ["file.testapp2", "syslog.testapp2"] |
| 47 | +``` |
| 48 | + |
| 49 | +### Example usage |
| 50 | + |
| 51 | +```julia |
| 52 | +julia> using LogCompose, Logging, LogRoller, SyslogLogging, LoggingExtras |
| 53 | + |
| 54 | +julia> logger1 = LogCompose.logger("testconfig.toml", "testapp1"); |
| 55 | + |
| 56 | +julia> typeof(logger1) |
| 57 | +TeeLogger{Tuple{RollingLogger,SyslogLogger}} |
| 58 | + |
| 59 | +julia> logger2 = LogCompose.logger("testconfig.toml", "testapp2"); |
| 60 | + |
| 61 | +julia> typeof(logger2) |
| 62 | +TeeLogger{Tuple{RollingLogger,SyslogLogger}} |
| 63 | + |
| 64 | +julia> first(logger1.loggers).stream.filename |
| 65 | +"/tmp/testapp1.log" |
| 66 | + |
| 67 | +julia> first(logger2.loggers).stream.filename |
| 68 | +"/tmp/testapp2.log" |
| 69 | + |
| 70 | +julia> first(logger2.loggers).stream.nfiles |
| 71 | +10 |
| 72 | + |
| 73 | +julia> with_logger(logger1) do |
| 74 | + @info("hello from app1") |
| 75 | + end |
| 76 | + |
| 77 | +shell> cat /tmp/testapp1.log |
| 78 | +┌ Info: 2020-04-02T12:03:03.588: hello from app1 |
| 79 | +└ @ Main REPL[13]:2 |
| 80 | + |
| 81 | +julia> with_logger(logger2) do |
| 82 | + @info("hello from app2") |
| 83 | + end |
| 84 | + |
| 85 | +shell> cat /tmp/testapp2.log |
| 86 | +┌ Info: 2020-04-02T12:04:13.156: hello from app2 |
| 87 | +└ @ Main REPL[15]:2 |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | +### Loggers Supported |
| 92 | + |
| 93 | +LogCompose supports the following types of loggers at the moment: |
| 94 | + |
| 95 | +- Logging.SimpleLogger |
| 96 | +- LoggingExtras.TeeLogger |
| 97 | +- LogRoller.RollingLogger |
| 98 | +- SyslogLogging.SyslogLogger |
| 99 | + |
| 100 | +### Plugging in other Loggers |
| 101 | + |
| 102 | +Support for a new logger can be added by providing an implementation of `LogCompose.logcompose` for the target logger type. |
| 103 | +This can be done in user code or in a package other than LogCompose. The implementation needs to be of the following form: |
| 104 | + |
| 105 | +```julia |
| 106 | +function LogCompose.logcompose(::Type{MyLoggerType}, |
| 107 | + config::Dict{String,Any}, # config: the entire logging configuration file |
| 108 | + logger_config::Dict{String,Any}) # logger_config: configuration relevant for the |
| 109 | + # section specified to `LogCompose.logger` |
| 110 | + # with the hierarchy flattened out |
| 111 | + # provides support for MyLoggerType in LogCompose |
| 112 | +end |
| 113 | +``` |
| 114 | + |
0 commit comments