-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathLogging.jl
146 lines (126 loc) · 3.52 KB
/
Logging.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# This file is a part of Julia. License is MIT: https://julialang.org/license
"""
Utilities for capturing, filtering and presenting streams of log events.
Normally you don't need to import `Logging` to create log events; for this
the standard logging macros such as `@info` are already exported by `Base`
and available by default.
"""
module Logging
using StyledStrings
# Import the CoreLogging implementation into Logging as new const bindings.
# Doing it this way (rather than with import) makes these symbols accessible to
# tab completion.
for sym in [
:LogLevel,
:AbstractLogger,
:NullLogger,
:handle_message, :shouldlog, :min_enabled_level, :catch_exceptions,
Symbol("@debug"),
Symbol("@info"),
Symbol("@warn"),
Symbol("@error"),
Symbol("@logmsg"),
:custom_log_levels,
:with_logger,
:current_logger,
:global_logger,
:disable_logging,
:SimpleLogger]
@eval const $sym = Base.CoreLogging.$sym
end
"""
@create_log_macro(name::Symbol, level::Int, face::Union{Symbol, StyledStrings.Face})
Creates a custom log macro like `@info`, `@warn` etc. with a given `name`,
`level` to be displayed with `face`. The macro created is named with the
lowercase form of `name` but the given form is used for the printing.
```julia-repl
julia> @create_log_macro(:MyLog, 200, :magenta)
@mylog (macro with 1 method)
julia> @mylog "hello"
[ MyLog: hello
```
"""
macro create_log_macro(name, level, color)
macro_name = Symbol(lowercase(string(name)))
macro_string = QuoteNode(name)
loglevel = LogLevel(level)
if loglevel in (BelowMinLevel, Debug, Info, Warn, Error, AboveMaxLevel)
throw(ArgumentError("Cannot use the same log level as a built in log macro"))
end
if haskey(custom_log_levels, loglevel)
throw(ArgumentError("Custom log macro already exists for given log level"))
end
quote
$(custom_log_levels)[$(esc(loglevel))] = ($(macro_string), $(esc(color)))
macro $(esc(macro_name))(exs...)
$(Base.CoreLogging.logmsg_code)(($(Base.CoreLogging.@_sourceinfo))..., $(esc(loglevel)), exs...)
end
end
end
# LogLevel aliases (re-)documented here (JuliaLang/julia#40978)
"""
Debug
Alias for [`LogLevel(-1000)`](@ref LogLevel).
"""
const Debug = Base.CoreLogging.Debug
"""
Info
Alias for [`LogLevel(0)`](@ref LogLevel).
"""
const Info = Base.CoreLogging.Info
"""
Warn
Alias for [`LogLevel(1000)`](@ref LogLevel).
"""
const Warn = Base.CoreLogging.Warn
"""
Error
Alias for [`LogLevel(2000)`](@ref LogLevel).
"""
const Error = Base.CoreLogging.Error
"""
BelowMinLevel
Alias for [`LogLevel(-1_000_001)`](@ref LogLevel).
"""
const BelowMinLevel = Base.CoreLogging.BelowMinLevel
"""
AboveMaxLevel
Alias for [`LogLevel(1_000_001)`](@ref LogLevel).
"""
const AboveMaxLevel = Base.CoreLogging.AboveMaxLevel
using Base.CoreLogging:
closed_stream
export
AbstractLogger,
LogLevel,
NullLogger,
@debug,
@info,
@warn,
@error,
@logmsg,
@create_log_macro,
with_logger,
current_logger,
global_logger,
disable_logging,
SimpleLogger,
ConsoleLogger,
BelowMinLevel,
Debug,
Info,
Warn,
Error,
AboveMaxLevel
include("ConsoleLogger.jl")
# The following are also part of the public API, but not exported:
#
# 1. Log levels:
# BelowMinLevel, Debug, Info, Warn, Error, AboveMaxLevel,
#
# 2. AbstractLogger message related functions:
# handle_message, shouldlog, min_enabled_level, catch_exceptions,
function __init__()
global_logger(ConsoleLogger())
end
end