For more information including compatibility, examples and test cases, see https://github.com/petercrlane/r7rs-libs
1. Logger: (import (robin logger))
The Logger library is used to output messages to an output port. Each message is given a level, and only messages above a certain set level will be output to the port. This allows the developer to control the level of detail output by the program. This logger library uses the same levels as Ruby’s Logger class:
-
unknown: An unknown message that should always be logged.
-
fatal: An unhandleable error that results in a program crash.
-
error: A handleable error condition.
-
warn: A warning.
-
info: Generic (useful) information about system operation.
-
debug: Low-level information for developers.
1.1. new-logger
new-logger
creates a new logger object. It accepts one argument: if the argument
is a port, the logged output will be directed at that port; if the argument is a string, then the
string will be treated as a filename, and the logger will open the file and send output to it.
Use (current-input-port)
to set the logger to output to the terminal.
1.2. logger?
logger?
checks if the given object is a logger object or not.
1.3. log-close
log-close
used to close up the logger. This only has an effect if the logger was
created with a string, and has opened its own output file: the file port created by the logger is
then closed.
1.4. log-add
log-add
is used to output a message. It accepts three arguments:
-
logger
is the log object to use for the output -
msg
is the message to output, and must be a string -
level
is the log level to use for the output
Usually you will use one of the functions described in the next subsection, but sometimes it is useful for the program to decide which log level to output a message at.
1.5. log-unknown log-fatal log-error log-warn log-info log-debug
log-unknown
log-fatal
log-error
log-warn
log-info
log-debug
all accept two arguments, the log object and a message to output. The message is
output if the log level mentioned in the function name is a valid one given the log object’s current log level.
1.6. log-level
log-level
accepts two arguments, a log object and a log level, and sets the logger’s level.
1.7. log-unknown? log-fatal? log-error? log-warn? log-info? log-debug?
log-unknown?
log-fatal?
log-error?
log-warn?
log-info?
log-debug?
all accept one argument, the log object, and return true if a log message at the given
level would be output by the log object.
1.8. Example
The following example illustrates how these functions may be used:
(define log (new-logger "log1.txt")) ; <1> (log-level log 'info) ; <2> (log-info log "some information logged") ; <3> (log-debug log "this will be ignored") ; <4> (log-level log 'debug) ; <5> (log-debug log "but this included") ; <6> (log-close log) ; <7>
-
Create a new logger, output to the given file
-
Sets the log level to "info"
-
Outputs a line at "info" level, which will be output
-
Tries to output a line at "debug" level, but this will be ignored as it is lower than the set level
-
Sets the log level to "debug"
-
And now the log message at "debug" level will be output
-
Finally, close the logger, which closes the file port it opened
The output from the above example is sent to the file "log1.txt", which contains two lines. Notice the log level precedes each message.
$ more log1.txt
info: some information logged
debug: but this included