Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new loggers inherit root logger properties #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ julia> son_logger = Logger("Son", parent=mum_logger);
julia> son_logger.level
INFO
```
If during the logger creation the `parent` parameter is not specified
then the logger inherits all properties of the `root` logger
unless specified otherwise explicitly.

```julia
julia> using Logging

julia> Logging.configure(level=DEBUG) # root has DEBUG level
Logger(root,DEBUG,TTY(open, 0 bytes waiting),root)

julia> logger1 = Logger("logger1") # logger1 has DEBUG level as well
Logger(logger1,DEBUG,TTY(open, 0 bytes waiting),root)

julia> logger2 = Logger("logger2", level=INFO) # logger2 has INFO level
Logger(logger2,INFO,TTY(open, 0 bytes waiting),root)
```

Notes
-----
Expand Down
10 changes: 5 additions & 5 deletions src/Logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Logging

using Compat

import Base: show
import Base: show, info, warn

export debug, info, warn, err, critical, log,
@debug, @info, @warn, @err, @error, @critical, @log,
Expand Down Expand Up @@ -31,13 +31,13 @@ type Logger
Logger(name::String, level::LogLevel, output::IO) = (x = new(); x.name = name; x.level=level; x.output=output; x.parent=x)
end

show(io::IO, logger::Logger) = print(io, "Logger(", join([logger.name,
logger.level,
show(io::IO, logger::Logger) = print(io, "Logger(", join([logger.name,
logger.level,
logger.output,
logger.parent.name], ","), ")")

const _root = Logger("root", WARNING, STDERR)
Logger(name::String;args...) = configure(Logger(name, WARNING, STDERR, _root); args...)
Logger(name::String;args...) = configure(Logger(name, _root.level, _root.output, _root); args...)
Logger() = Logger("logger")

for (fn,lvl,clr) in ((:debug, DEBUG, :cyan),
Expand Down Expand Up @@ -70,7 +70,7 @@ function configure(logger=_root; args...)
logger.output = parent.output
end
end

for (tag, val) in args
tag == :io ? (logger.output = val::IO) :
tag == :output ? (logger.output = val::IO) :
Expand Down
30 changes: 30 additions & 0 deletions test/hierarchy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module TestHierarchy
using Base.Test
using Logging

# configre root logger
output = IOBuffer()
Logging.configure(level=DEBUG, io = output)
root = Logging._root

loggerA = "levelA"
level1A = Logger(loggerA)

loggerB = "levelB"
level1B = Logger(loggerB, level = INFO)

logger2 = "level2"
level2 = Logger(logger2, parent=level1B)

# test hierarchy
@test root.parent == root
@test level1A.parent == root
@test level1A.parent == root
@test level2.parent == level1B

# test properties
@test level1A.level == root.level
@test level1B.level == INFO
@test level2.level == level1B.level

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include("log_test.jl")
include("macro_test1.jl")
include("macro_test2.jl")
include("macro_test3.jl")
include("hierarchy.jl")