Skip to content

Commit

Permalink
Merge pull request #7 from colinfang/dev
Browse files Browse the repository at this point in the history
Support Julia v0.7
  • Loading branch information
colinfang authored Jan 11, 2019
2 parents 6b7be30 + f7ecf9a commit 363e6b2
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- 1.0
- nightly
notifications:
email: false
12 changes: 12 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = "MiniLogging"
uuid = "f59402ec-0262-5707-a561-770af94bc5a6"
keywords = ["logging", "log"]
license = "MIT"
desc = "Mini hierarchical logging system like Python logging"
version = "0.2"

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

[targets]
test = ["Test"]
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
julia 0.6 0.7-
julia 0.7
16 changes: 13 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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

# Note

- v0.1 is last release that supports Julia v0.6.
- Users have to explicitly export logging macros because they are already used by `Base`.
```julia
using MiniLogging
# Explicitly shadow Base
using MiniLogging: @debug, @info, @warn, @error, @critical
```
## Overview

This is a Julia equivalent of Python logging package. It provides a basic hierarchical logging system.
Expand All @@ -16,7 +25,7 @@ When dealing with multiple nested modules, the experience with the existing Juli
## Features

- The logger hierarchy is defined by the logger name, which is a dot-separated string (e.g. `"a.b"`).
- Simply use `get_logger(current_module())` to maintain a hierarchy.
- Simply use `get_logger(@__MODULE__)` to maintain a hierarchy.
- All loggers inherit settings from their ancestors up to the root by default.
- Most of the time it is sufficient to set the root logger config only.
- Colors & logging levels are customizable.
Expand All @@ -32,6 +41,7 @@ export @debug, @info, @warn, @error, @critical

```julia
julia> using MiniLogging
julia> using MiniLogging: @debug, @info, @warn, @error, @critical
# Get root logger.
# Nothing appears as we haven't set any config on any loggers.
Expand Down Expand Up @@ -91,10 +101,10 @@ julia> @debug(logger2, "Hello", " world")
- Log to `file_name`.

```julia
# Log to both `STDERR` & `foo`.
# Log to both `STDERR` & a file.
basic_config(MiniLogging.INFO, "foo")
root_logger = get_logger()
push!(root_logger.handlers, MiniLogging.Handler(STDERR, "%Y-%m-%d %H:%M:%S”))
push!(root_logger.handlers, MiniLogging.Handler(stderr, "%Y-%m-%d %H:%M:%S”))
```


Expand Down
6 changes: 3 additions & 3 deletions src/Hierarchy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export Tree, parent_node
include("ancestors.jl")

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

immutable Tree
struct Tree
parent_node::Dict{String, String}
# node => descendants
# @assert has_node(t, descendant)
Expand Down
10 changes: 5 additions & 5 deletions src/MiniLogging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ for (value, symbol) in DEFINED_LEVELS
end


type Handler
mutable struct Handler
output::IO
date_format::String
end

type Logger
mutable struct Logger
name::String
level::LogLevel
handlers::Vector{Handler}
Expand Down Expand Up @@ -114,7 +114,7 @@ end

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

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

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

function _log(
logger::Logger, level::LogLevel, color::Symbol,
Expand Down
34 changes: 21 additions & 13 deletions src/ancestors.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
immutable Ancestors
struct Ancestors
node::String

function Ancestors(node::String)
if endswith(node, '.')
if endswith(node, '.') || startswith(node, '.')
error("Invalid node $node - Cannot end with `.`")
end
new(node)
end
end

Base.start(x::Ancestors) = endof(x.node)

function Base.next(a::Ancestors, state)
node = a.node
i = rsearch(node, '.', state)
if i > 1
state = prevind(node, i)
function Base.iterate(x::Ancestors, state=nothing)
node = x.node
if state == 0 || isempty(node)
return nothing
end

if state == nothing
state = lastindex(node)
end

r = findprev(".", node, state)
if r == nothing
return "", 0
else
@assert r.start == r.stop
state = prevind(node, r.start)
if node[state] == '.'
error("Invalid node $node - Cannot have `..`")
end
node[1:state], state
else
"", 0
return node[1:state], state
end
end

Base.done(::Ancestors, state) = state < 1
Base.iteratorsize(::Type{Ancestors}) = Base.SizeUnknown()

Base.IteratorSize(::Type{Ancestors}) = Base.SizeUnknown()
Base.eltype(::Type{Ancestors}) = String

is_ancestor_or_self(ancestor::String, descendant::String) = startswith(descendant, ancestor)
9 changes: 3 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Base.Test
using Test
using MiniLogging
using MiniLogging: @debug, @info, @warn, @error, @critical
using MiniLogging.Hierarchy
using MiniLogging.Hierarchy: Ancestors

@test collect(Ancestors("")) == []
@test collect(Ancestors("a")) == [""]
@test collect(Ancestors("a.bb.ccc")) == ["a.bb", "a", ""]
@test collect(Ancestors(".a.bb.ccc")) == [".a.bb", ".a", ""]
@test collect(Ancestors("❤.❤❤.❤❤❤")) == ["❤.❤❤", "" , ""]
@test_throws ErrorException collect(Ancestors(".a.bb.ccc"))
@test_throws ErrorException collect(Ancestors("a.b.cc."))
@test_throws ErrorException collect(Ancestors("a.b..cc"))

Expand All @@ -30,10 +31,6 @@ push!(t, "a.b.❤.d")
@test parent_node(t, "a.b.❤.d") == "a.b"
push!(t, "a")
@test parent_node(t, "a") == ""
push!(t, ".a.b")
@test parent_node(t, ".a.b") == ""
push!(t, ".a.b.c")
@test parent_node(t, ".a.b.c") == ".a.b"
push!(t, "")
@test parent_node(t, "") == ""

Expand Down

0 comments on commit 363e6b2

Please sign in to comment.