Skip to content

Commit ad32bcc

Browse files
authored
Merge pull request #4 from tanmaykm/tan/misc
separate out loggers other than stdlib Logging
2 parents 82da4ee + 347c064 commit ad32bcc

File tree

9 files changed

+237
-217
lines changed

9 files changed

+237
-217
lines changed

.appveyor.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
environment:
2+
matrix:
3+
- julia_version: 1.0
4+
- julia_version: 1.1
5+
- julia_version: 1.2
6+
- julia_version: 1.3
7+
- julia_version: 1.4
8+
- julia_version: nightly
9+
10+
platform:
11+
- x86 # 32-bit
12+
- x64 # 64-bit
13+
14+
# Uncomment the following lines to allow failures on nightly julia
15+
# (tests will run but not make your overall status red)
16+
matrix:
17+
allow_failures:
18+
- julia_version: nightly
19+
20+
#branches:
21+
# only:
22+
# - master
23+
# - /release-.*/
24+
25+
notifications:
26+
- provider: Email
27+
on_build_success: false
28+
on_build_failure: false
29+
on_build_status_changed: false
30+
31+
install:
32+
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1"))
33+
34+
build_script:
35+
- echo "%JL_BUILD_SCRIPT%"
36+
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"
37+
38+
test_script:
39+
- echo "%JL_TEST_SCRIPT%"
40+
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"
41+
42+
# # Uncomment to support code coverage upload. Should only be enabled for packages
43+
# # which would have coverage gaps without running on Windows
44+
# on_success:
45+
# - echo "%JL_CODECOV_SCRIPT%"
46+
# - C:\julia\bin\julia -e "%JL_CODECOV_SCRIPT%"

Project.toml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
name = "LogCompose"
22
uuid = "8416b438-731e-11ea-2421-05f642269042"
33
keywords = ["configure", "compose", "logging", "logger"]
4-
authors = ["tan <[email protected]>"]
4+
authors = ["Tanmay Mohapatra <[email protected]>"]
55
license = "MIT"
66
desc = "Compose loggers and logger ensembles declaratively using configuration files"
7-
version = "0.1.2"
7+
version = "0.2.0"
88

99
[deps]
1010
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1111
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
12-
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
13-
SyslogLogging = "0c4b0c42-68ec-11ea-3bc9-e7fb6e00ea0f"
14-
LogRoller = "c41e01d8-14e5-11ea-185b-e7eabed7be4b"
15-
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
1612

1713
[compat]
1814
julia = "1"
19-
SyslogLogging = "0.1"
20-
LoggingExtras = "0.4"
21-
LogRoller = "0.3"
2215

2316
[extras]
2417
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

README.md

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,70 @@
11
# LogCompose
22

33
[![Build Status](https://travis-ci.org/tanmaykm/LogCompose.jl.png)](https://travis-ci.org/tanmaykm/LogCompose.jl)
4+
[![Build Status](https://ci.appveyor.com/api/projects/status/github/tanmaykm/LogCompose.jl?branch=master&svg=true)](https://ci.appveyor.com/project/tanmaykm/logroller-jl/branch/master)
45
[![Coverage Status](https://coveralls.io/repos/github/tanmaykm/LogCompose.jl/badge.svg?branch=master)](https://coveralls.io/github/tanmaykm/LogCompose.jl?branch=master)
56

67
Provides a way to specify hierarchical logging configuration in a file.
78

89
Configuration file is in the form of a TOML file. Configuration sections are named,
910
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+
Sections inherit parameter values from preceeding sections and can override them as well.
1112
Loggers can be constructed by providing the name of a section.
1213

13-
### Example configuration
14+
[Here](example.toml) is what a configuration that allows logging to several types of loggers may look like.
15+
16+
## Plugging in a Logger
17+
18+
Support for a logger can be added by providing an implementation of `LogCompose.logcompose` for the target logger type.
19+
The implementation needs to be of the following form:
20+
21+
```julia
22+
function LogCompose.logcompose(::Type{MyLoggerType},
23+
config::Dict{String,Any}, # config: the entire logging configuration file
24+
logger_config::Dict{String,Any}) # logger_config: configuration relevant for the
25+
# section specified to `LogCompose.logger`
26+
# with the hierarchy flattened out
27+
# provides support for MyLoggerType in LogCompose
28+
end
29+
```
30+
31+
For complete examples, refer to any of the existing implementations listed below.
32+
33+
## Loggers Supported
34+
35+
LogCompose has in-built support for the loggers provided in the stdlib logging package.
36+
They are listed below with example configuration sections illustrating parameters they accept.
37+
38+
- Logging.SimpleLogger
39+
```
40+
[loggers.simple]
41+
type = "Logging.SimpleLogger"
42+
# min_level = "Debug" # Debug, Info (default) or Error
43+
stream = "simple.log" # file to log to
44+
```
45+
- Logging.ConsoleLogger
46+
```
47+
[loggers.console]
48+
type = "Logging.ConsoleLogger"
49+
# min_level = "Debug" # Debug, Info (default) or Error
50+
stream = "stdout" # stdout (default), stderr or a filepath
51+
```
52+
- Logging.NullLogger
53+
```
54+
[loggers.null]
55+
type = "Logging.NullLogger"
56+
```
57+
58+
There are external packages that provide support for a few other types of loggers as well:
59+
60+
- LoggingExtras: [LoggingExtrasCompose.jl](https://github.com/tanmaykm/LoggingExtrasCompose.jl)
61+
- LogRoller: [LogRollerCompose.jl](https://github.com/tanmaykm/LogRollerCompose.jl)
62+
- SyslogLogging: [SyslogLoggingCompose.jl](https://github.com/tanmaykm/SyslogLoggingCompose.jl)
63+
64+
65+
## Examples
66+
67+
Here is an example configuration using multiple logger types, from different logging packages.
1468
1569
```toml
1670
[file]
@@ -46,10 +100,16 @@ type = "LoggingExtras.TeeLogger"
46100
destinations = ["file.testapp2", "syslog.testapp2"]
47101
```
48102

49-
### Example usage
103+
And below is a snippet of Julia code that make use of this configuration:
50104

51105
```julia
52-
julia> using LogCompose, Logging, LogRoller, SyslogLogging, LoggingExtras
106+
julia> using LogCompose, Logging
107+
108+
julia> using LogRoller, LogRollerCompose
109+
110+
julia> using SyslogLogging, SyslogLoggingCompose
111+
112+
julia> using LoggingExtras, LoggingExtrasCompose
53113

54114
julia> logger1 = LogCompose.logger("testconfig.toml", "testapp1");
55115

@@ -87,30 +147,3 @@ shell> cat /tmp/testapp2.log
87147
└ @ Main REPL[15]:2
88148

89149
```
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-
- LogRoler.RollingFileWriterTee
99-
- LogRoller.RollingFileWriter
100-
- SyslogLogging.SyslogLogger
101-
102-
### Plugging in other Loggers
103-
104-
Support for a new logger can be added by providing an implementation of `LogCompose.logcompose` for the target logger type.
105-
This can be done in user code or in a package other than LogCompose. The implementation needs to be of the following form:
106-
107-
```julia
108-
function LogCompose.logcompose(::Type{MyLoggerType},
109-
config::Dict{String,Any}, # config: the entire logging configuration file
110-
logger_config::Dict{String,Any}) # logger_config: configuration relevant for the
111-
# section specified to `LogCompose.logger`
112-
# with the hierarchy flattened out
113-
# provides support for MyLoggerType in LogCompose
114-
end
115-
```
116-

example.toml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[loggers.default]
2+
type = "Logging.SimpleLogger"
3+
# min_level = "Debug" # Debug, Info (default) or Error
4+
stream = "/tmp/default.log" # stdout (default), stderr or a filepath
5+
6+
[loggers.file]
7+
type = "LogRoller.RollingLogger"
8+
# sizelimit = 10240000
9+
# nfiles = 5
10+
# min_level = "Debug" # Debug, Info (default) or Error
11+
# timestamp_identifier = "time"
12+
13+
[loggers.syslog]
14+
type = "SyslogLogging.SyslogLogger"
15+
# min_level = "Info" # default is Info
16+
# facility = "user" # one of the facility types listed in Syslogs.jl (default is user)
17+
# server_type = "tcp" # tcp or udp or local (default)
18+
# server_host =
19+
# server_port =
20+
# lock = false # whether to lock the syslog facility while logging
21+
22+
[loggers.stackdriver]
23+
type = "StackDriver.StackdriverLogger"
24+
min_level = "Info" # default is Info
25+
project_name = "myapp"
26+
log_name = "myapp.log"
27+
key_file_path = "keyfile"
28+
access_token = "accesstoken"
29+
30+
[loggers.file.testapp]
31+
filename = "/tmp/testapp.log"
32+
33+
[loggers.syslog.testapp]
34+
identity = "testapp"
35+
36+
[loggers.stackdriver.testapp]
37+
log_name = "testapp"
38+
39+
[loggers.testapp]
40+
type = "LoggingExtras.TeeLogger"
41+
destinations = ["file.testapp", "syslog.testapp"]
42+
43+
[loggers.testapptee]
44+
type = "LogRoller.RollingFileWriterTee"
45+
destination = "file.testapp"
46+
filename = "/tmp/testapptee.log"
47+
# sizelimit = 10240000
48+
# nfiles = 5
49+
# assumed_level = "Debug" # Debug, Info (default) or Error
50+
51+
[loggers.plainfile]
52+
type = "LogRoller.RollingFileWriter"
53+
filename = "/tmp/testplain.log"
54+
# sizelimit = 10240000
55+
# nfiles = 5
56+
# assumed_level = "Debug" # Debug, Info (default) or Error

src/LogCompose.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module LogCompose
22

33
using Pkg, Logging
44

5-
export logcompose
6-
75
include("compose.jl")
86
include("connectors.jl")
97

src/compose.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Logging, LogRoller, SyslogLogging, LoggingExtras
1+
using Logging
22

33
name_parts(loggername) = split(loggername, '.')
44

@@ -42,16 +42,29 @@ function get_type(s::String, topmodule::Module=@__MODULE__)
4242
end
4343
end
4444

45+
function get_type(s::String, modules::Vector{Module})
46+
L = length(modules)
47+
for idx in 1:L
48+
try
49+
return get_type(s, modules[idx])
50+
catch ex
51+
(idx == L) && rethrow(ex)
52+
end
53+
end
54+
end
55+
4556
logger(configfile::String, loggername::String; section::String="") = logger(configuration(configfile; section=section), name_parts(loggername))
4657
logger(config::Dict{String,Any}, loggername::String) = logger(config, name_parts(loggername))
4758
function logger(config::Dict{String,Any}, loggername::Vector)
4859
loggercfg = flatten(config, loggername)
4960
loggertypestr = loggercfg["type"]
50-
loggertype = get_type(loggertypestr)
61+
loggertopmodule = Base.eval(Main, Symbol(get(loggercfg, "topmodule", "Main")))
62+
modules = collect(Set([Main, loggertopmodule, @__MODULE__]))
63+
loggertype = get_type(loggertypestr, modules)
64+
5165
logcompose(loggertype, config, loggercfg)
5266
end
5367

54-
logcompose(::Type{T}, config::Dict{String,Any}, logger_config::Dict{String,Any}) where {T} = @error("logcompose not implemented for type $T")
68+
logcompose(::Type{T}, config::Dict{String,Any}, logger_config::Dict{String,Any}) where {T} = error("logcompose not implemented for type $T")
5569

5670
log_min_level(logger_config::Dict{String,Any}, default::String="Info") = getproperty(Logging, Symbol(get(logger_config, "min_level", default)))
57-
log_assumed_level(logger_config::Dict{String,Any}, default::String="Info") = getproperty(Logging, Symbol(get(logger_config, "assumed_level", default)))

0 commit comments

Comments
 (0)