Skip to content

Commit

Permalink
write logs to stderr instead of stdout; fixes nim-lang#9547 (nim-lang…
Browse files Browse the repository at this point in the history
  • Loading branch information
xmonader authored and narimiran committed Oct 31, 2018
1 parent 2041070 commit eece8b5
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/pure/logging.nim
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type

ConsoleLogger* = ref object of Logger ## logger that writes the messages to the
## console
useStderr*: bool ## will send logs into Stderr if set

when not defined(js):
type
Expand Down Expand Up @@ -150,16 +151,20 @@ method log*(logger: ConsoleLogger, level: Level, args: varargs[string, `$`]) =
{.emit: "console.log(`cln`);".}
else:
try:
writeLine(stdout, ln)
if level in {lvlError, lvlFatal}: flushFile(stdout)
var handle = stdout
if logger.useStderr:
handle = stderr
writeLine(handle, ln)
if level in {lvlError, lvlFatal}: flushFile(handle)
except IOError:
discard

proc newConsoleLogger*(levelThreshold = lvlAll, fmtStr = defaultFmtStr): ConsoleLogger =
proc newConsoleLogger*(levelThreshold = lvlAll, fmtStr = defaultFmtStr, useStderr=false): ConsoleLogger =
## Creates a new console logger. This logger logs to the console.
new result
result.fmtStr = fmtStr
result.levelThreshold = levelThreshold
result.useStderr = useStderr

when not defined(js):
method log*(logger: FileLogger, level: Level, args: varargs[string, `$`]) =
Expand Down

0 comments on commit eece8b5

Please sign in to comment.