Reputation: 18450
By default, the package logging
only processes messages at level INFO. Now I want to log DEBUG messages, too. From the tutorial I adapted the following snippet:
library(logging)
logReset()
addHandler(writeToConsole)
setLevel("DEBUG", getHandler("writeToConsole"))
getHandler("writeToConsole")[["level"]]==loglevels["DEBUG"]
#TRUE
loginfo("this is an info")
#2018-06-15 13:04:40 INFO::this is an info
logdebug("this is a debug note.")
# nothing happens
What am I doing wrong?
Upvotes: 2
Views: 875
Reputation: 3200
In the context of logging
package, the criticality levels are set for both, for the logger and for the handler objects. In your code you use global logger and a custom handler, where the minimal level of the logger conditions the handling levels is 'INFO'.
After logReset()
you reset the root logger and remove all default handlers:
logReset()
getLogger()[['level']] # accessing the root logger
# INFO
# 20
with(getLogger(logger=''), names(handlers))
# character(0)
Hence your code:
library(logging)
logReset()
addHandler(writeToConsole)
setLevel("DEBUG", getHandler("writeToConsole"))
getHandler("writeToConsole")[["level"]]==loglevels["DEBUG"]
#TRUE
loginfo("this is an info")
# 2021-11-11 17:35:25 INFO::this is an info
logdebug("this is a debug note.")
# nothing
can be adjusted as follows (run after execution of your script):
getLogger()[['level']]
# INFO
# 20
setLevel('DEBUG') # or setLevel('DEBUG', container = '') where it is clear that the root logger is being accessed
getLogger()[['level']]
# DEBUG
# 10
logdebug("this is a debug note.")
# 2021-11-11 17:36:55 DEBUG::this is a debug note.
Another simpler way is to use basicConfig()
for the root logger setting it like:
logReset()
basicConfig('DEBUG')
loginfo("this is an info")
# 2021-11-11 17:37:43 INFO::this is an info
logdebug("this is a debug note")
# 2021-11-11 17:37:43 DEBUG::this is a debug note
or yet another option, with dedicated logger object:
logReset()
addHandler(writeToConsole, logger='my_logger')
lrc <- getLogger('my_logger')
lrc$level
# NOTSET
# 0
lrc$setLevel('DEBUG')
lrc$level
# DEBUG
# 10
setLevel('INFO', lrc$handlers$writeToConsole)
lrc$debug("test")
# nothing
setLevel('DEBUG', lrc$handlers$writeToConsole)
lrc$debug("test")
# 2021-11-11 17:38:32 DEBUG:my_logger:test
Upvotes: 1