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

Add generic function loggerstream #28229

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 17 additions & 0 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export
@logmsg,
with_logger,
current_logger,
loggerstream,
global_logger,
disable_logging,
SimpleLogger
Expand Down Expand Up @@ -83,6 +84,7 @@ min_enabled_level(::NullLogger) = AboveMaxLevel
shouldlog(::NullLogger, args...) = false
handle_message(::NullLogger, args...; kwargs...) =
error("Null logger handle_message() should not be called")
loggerstream(::NullLogger) = devnull


#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -488,6 +490,20 @@ is attached to the task.
"""
current_logger() = current_logstate().logger

"""
loggerstream()

Return the underlying IO stream of the current logger.
"""
loggerstream() = loggerstream(current_logger())

"""
loggerstream(logger::AbstractLogger)

Return the IO stream of `logger`.
"""
loggerstream(::AbstractLogger)


#-------------------------------------------------------------------------------
# SimpleLogger
Expand All @@ -503,6 +519,7 @@ struct SimpleLogger <: AbstractLogger
message_limits::Dict{Any,Int}
end
SimpleLogger(stream::IO=stderr, level=Info) = SimpleLogger(stream, level, Dict{Any,Int}())
loggerstream(logger::SimpleLogger) = logger.stream

shouldlog(logger::SimpleLogger, level, _module, group, id) =
get(logger.message_limits, id, 1) > 0
Expand Down
2 changes: 2 additions & 0 deletions stdlib/Logging/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ Event processing is controlled by overriding functions associated with
| [`Logging.min_enabled_level`](@ref) | | Lower bound for log level of accepted events |
| **Optional methods** | **Default definition** | **Brief description** |
| [`Logging.catch_exceptions`](@ref) | `true` | Catch exceptions during event evaluation |
| [`Logging.loggerstream`](@ref) | | Return the IO stream of the logger |


```@docs
Expand All @@ -224,6 +225,7 @@ Logging.shouldlog
Logging.min_enabled_level
Logging.catch_exceptions
Logging.disable_logging
Logging.loggerstream
```

### Using Loggers
Expand Down
2 changes: 2 additions & 0 deletions stdlib/Logging/src/ConsoleLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function ConsoleLogger(stream::IO=stderr, min_level=Info;
show_limited, right_justify, Dict{Any,Int}())
end

loggerstream(logger::ConsoleLogger) = logger.stream

shouldlog(logger::ConsoleLogger, level, _module, group, id) =
get(logger.message_limits, id, 1) > 0

Expand Down
2 changes: 2 additions & 0 deletions stdlib/Logging/src/Logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Base.CoreLogging:
@logmsg,
with_logger,
current_logger,
loggerstream,
global_logger,
disable_logging,
SimpleLogger
Expand All @@ -37,6 +38,7 @@ export
@logmsg,
with_logger,
current_logger,
loggerstream,
global_logger,
disable_logging,
SimpleLogger,
Expand Down
9 changes: 9 additions & 0 deletions stdlib/Logging/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ import Logging: min_enabled_level, shouldlog, handle_message
\e[36m\e[1m└ \e[22m\e[39m\e[90mSUFFIX\e[39m
"""

# loggerstream
io = IOBuffer()
logger = ConsoleLogger(io)
with_logger(logger) do
print(loggerstream(), "Hello,")
print(loggerstream(logger), " world!")
end
@test String(take!(io)) == "Hello, world!"

end

end
18 changes: 18 additions & 0 deletions test/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,24 @@ end
"""
end

@testset "loggerstream" begin
# NullLogger
logger = NullLogger()
@test loggerstream(logger) == devnull
with_logger(logger) do
@test loggerstream() == devnull
end

# SimpleLogger
io = IOBuffer()
logger = SimpleLogger(io)
with_logger(logger) do
print(loggerstream(), "Hello,")
print(loggerstream(logger), " world!")
end
@test String(take!(io)) == "Hello, world!"
end

# Issue #26273
let m = Module(:Bare26273i, false)
Core.eval(m, :(import Base: @error))
Expand Down