Skip to content

Commit 363e6b2

Browse files
authored
Merge pull request #7 from colinfang/dev
Support Julia v0.7
2 parents 6b7be30 + f7ecf9a commit 363e6b2

File tree

8 files changed

+61
-32
lines changed

8 files changed

+61
-32
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.6
7+
- 0.7
8+
- 1.0
9+
- nightly
810
notifications:
911
email: false

Project.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name = "MiniLogging"
2+
uuid = "f59402ec-0262-5707-a561-770af94bc5a6"
3+
keywords = ["logging", "log"]
4+
license = "MIT"
5+
desc = "Mini hierarchical logging system like Python logging"
6+
version = "0.2"
7+
8+
[extras]
9+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
10+
11+
[targets]
12+
test = ["Test"]

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
julia 0.6 0.7-
1+
julia 0.7

Readme.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
[![Build Status](https://travis-ci.org/colinfang/MiniLogging.jl.svg?branch=master)](https://travis-ci.org/colinfang/MiniLogging.jl)
44

5+
# Note
6+
7+
- v0.1 is last release that supports Julia v0.6.
8+
- Users have to explicitly export logging macros because they are already used by `Base`.
9+
```julia
10+
using MiniLogging
11+
# Explicitly shadow Base
12+
using MiniLogging: @debug, @info, @warn, @error, @critical
13+
```
514
## Overview
615

716
This is a Julia equivalent of Python logging package. It provides a basic hierarchical logging system.
@@ -16,7 +25,7 @@ When dealing with multiple nested modules, the experience with the existing Juli
1625
## Features
1726
1827
- The logger hierarchy is defined by the logger name, which is a dot-separated string (e.g. `"a.b"`).
19-
- Simply use `get_logger(current_module())` to maintain a hierarchy.
28+
- Simply use `get_logger(@__MODULE__)` to maintain a hierarchy.
2029
- All loggers inherit settings from their ancestors up to the root by default.
2130
- Most of the time it is sufficient to set the root logger config only.
2231
- Colors & logging levels are customizable.
@@ -32,6 +41,7 @@ export @debug, @info, @warn, @error, @critical
3241
3342
```julia
3443
julia> using MiniLogging
44+
julia> using MiniLogging: @debug, @info, @warn, @error, @critical
3545
3646
# Get root logger.
3747
# Nothing appears as we haven't set any config on any loggers.
@@ -91,10 +101,10 @@ julia> @debug(logger2, "Hello", " world")
91101
- Log to `file_name`.
92102

93103
```julia
94-
# Log to both `STDERR` & `foo`.
104+
# Log to both `STDERR` & a file.
95105
basic_config(MiniLogging.INFO, "foo")
96106
root_logger = get_logger()
97-
push!(root_logger.handlers, MiniLogging.Handler(STDERR, "%Y-%m-%d %H:%M:%S”))
107+
push!(root_logger.handlers, MiniLogging.Handler(stderr, "%Y-%m-%d %H:%M:%S”))
98108
```
99109
100110

src/Hierarchy.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ export Tree, parent_node
55
include("ancestors.jl")
66

77
# - A node is a dot-separated string representing hierarchy.
8-
# - E.g. `""`, `"a"`, `".a.b"`, `"a.bb.ccc"`.
9-
# - `"a.b..c"` & `"a.b.c."` are not valid.
8+
# - E.g. `""`, `a`, `a.b`, `a.bb.ccc`.
9+
# - `a.b..c` & `a.b.c.` & `.a.b` are not valid.
1010
# A parent node is the nearest ancestor node in `Tree`, fallback to `""` of not found.
1111

12-
immutable Tree
12+
struct Tree
1313
parent_node::Dict{String, String}
1414
# node => descendants
1515
# @assert has_node(t, descendant)

src/MiniLogging.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ for (value, symbol) in DEFINED_LEVELS
2121
end
2222

2323

24-
type Handler
24+
mutable struct Handler
2525
output::IO
2626
date_format::String
2727
end
2828

29-
type Logger
29+
mutable struct Logger
3030
name::String
3131
level::LogLevel
3232
handlers::Vector{Handler}
@@ -114,7 +114,7 @@ end
114114

115115
function basic_config(level::LogLevel; date_format::String="%Y-%m-%d %H:%M:%S")
116116
ROOT.level = level
117-
handler = Handler(STDERR, date_format)
117+
handler = Handler(stderr, date_format)
118118
push!(ROOT.handlers, handler)
119119
end
120120

@@ -125,8 +125,8 @@ function basic_config(level::LogLevel, file_name::String; date_format::String="%
125125
push!(ROOT.handlers, handler)
126126
end
127127

128-
write_log{T<:IO}(output::T, color::Symbol, msg::AbstractString) = (print(output, msg); flush(output))
129-
write_log(output::Base.TTY, color::Symbol, msg::AbstractString) = Base.print_with_color(color, output, msg)
128+
write_log(output::T, color::Symbol, msg::AbstractString) where T <: IO = (print(output, msg); flush(output))
129+
write_log(output::Base.TTY, color::Symbol, msg::AbstractString) = Base.printstyled(output, msg, color=color)
130130

131131
function _log(
132132
logger::Logger, level::LogLevel, color::Symbol,

src/ancestors.jl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
1-
immutable Ancestors
1+
struct Ancestors
22
node::String
33

44
function Ancestors(node::String)
5-
if endswith(node, '.')
5+
if endswith(node, '.') || startswith(node, '.')
66
error("Invalid node $node - Cannot end with `.`")
77
end
88
new(node)
99
end
1010
end
1111

12-
Base.start(x::Ancestors) = endof(x.node)
1312

14-
function Base.next(a::Ancestors, state)
15-
node = a.node
16-
i = rsearch(node, '.', state)
17-
if i > 1
18-
state = prevind(node, i)
13+
function Base.iterate(x::Ancestors, state=nothing)
14+
node = x.node
15+
if state == 0 || isempty(node)
16+
return nothing
17+
end
18+
19+
if state == nothing
20+
state = lastindex(node)
21+
end
22+
23+
r = findprev(".", node, state)
24+
if r == nothing
25+
return "", 0
26+
else
27+
@assert r.start == r.stop
28+
state = prevind(node, r.start)
1929
if node[state] == '.'
2030
error("Invalid node $node - Cannot have `..`")
2131
end
22-
node[1:state], state
23-
else
24-
"", 0
32+
return node[1:state], state
2533
end
2634
end
2735

28-
Base.done(::Ancestors, state) = state < 1
29-
Base.iteratorsize(::Type{Ancestors}) = Base.SizeUnknown()
36+
37+
Base.IteratorSize(::Type{Ancestors}) = Base.SizeUnknown()
3038
Base.eltype(::Type{Ancestors}) = String
3139

3240
is_ancestor_or_self(ancestor::String, descendant::String) = startswith(descendant, ancestor)

test/runtests.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
using Base.Test
1+
using Test
22
using MiniLogging
3+
using MiniLogging: @debug, @info, @warn, @error, @critical
34
using MiniLogging.Hierarchy
45
using MiniLogging.Hierarchy: Ancestors
56

67
@test collect(Ancestors("")) == []
78
@test collect(Ancestors("a")) == [""]
89
@test collect(Ancestors("a.bb.ccc")) == ["a.bb", "a", ""]
9-
@test collect(Ancestors(".a.bb.ccc")) == [".a.bb", ".a", ""]
1010
@test collect(Ancestors("❤.❤❤.❤❤❤")) == ["❤.❤❤", "" , ""]
11+
@test_throws ErrorException collect(Ancestors(".a.bb.ccc"))
1112
@test_throws ErrorException collect(Ancestors("a.b.cc."))
1213
@test_throws ErrorException collect(Ancestors("a.b..cc"))
1314

@@ -30,10 +31,6 @@ push!(t, "a.b.❤.d")
3031
@test parent_node(t, "a.b.❤.d") == "a.b"
3132
push!(t, "a")
3233
@test parent_node(t, "a") == ""
33-
push!(t, ".a.b")
34-
@test parent_node(t, ".a.b") == ""
35-
push!(t, ".a.b.c")
36-
@test parent_node(t, ".a.b.c") == ".a.b"
3734
push!(t, "")
3835
@test parent_node(t, "") == ""
3936

0 commit comments

Comments
 (0)