simple-logger
TOC »
Author
Mario Domenech Goulart
Repository
https://github.com/mario-goulart/simple-logger
Requirements
None
Description
A very simple logger for CHICKEN.
API
Logging procedures
- log-info fmt #!rest argsprocedure
Log info messages using the configuration from the info-logger-config parameter.
fmt is a format string with placeholders (see the format string of printf) and args are values to replace placeholders in the format string. A new line character is implicitly added to fmt.
Examples:
(log-info "hey") (let ((some-variable "blah")) (log-info "The value of some-variable is ~a" some-variable))
- log-warning fmt #!rest argsprocedure
Log warning messages using the configuration from the warning-logger-config parameter.
fmt is a format string with placeholders (see the format string of printf) and args are values to replace placeholders in the format string. A new line character is implicitly added to fmt.
Examples:
(log-warning "You've been warned!") (let ((some-variable "blah")) (log-warning "The value of some-variable is ~a" some-variable))
- log-error fmt #!rest argsprocedure
Log error messages using the configuration from the error-logger-config parameter.
fmt is a format string with placeholders (see the format string of printf) and args are values to replace placeholders in the format string. A new line character is implicitly added to fmt.
Examples:
(log-error "KABOOM!") (let ((some-variable "blah")) (log-error "The value of some-variable is ~a" some-variable))
- log-debug fmt #!rest argsprocedure
Log debug messages using the configuration from the debug-logger-config parameter.
fmt is a format string with placeholders (see the format string of printf) and args are values to replace placeholders in the format string. A new line character is implicitly added to fmt.
Examples:
(log-debug "Here!") (let ((some-variable "blah")) (log-debug "The value of some-variable is ~a" some-variable))
- die! fmt #!rest argsprocedure
Simple alias to a call to (log-error fmt . args) followed by a call to (exit 1). A new line character is implicitly added to fmt.
Example:
(die! "Goodbye, cruel world.")
Configuration parameters
- log-level #!optional default=30parameter
Default log level to be considered by the logging procedures. Lower level log messages have lower "priority".
Logger procedures whose configurations have a level attribute with a value greater than or equal to log-level will print to the configured port.
With the default configuration, warning and error messages are printed.
- (debug-logger-config [default=(make-logger-config prefix: (lambda () "[DEBUG] ") port: (current-error-port) level: 10)])parameter
Default configuration for the debug logger.
- (info-logger-config [default=(make-logger-config prefix: (lambda () "[INFO] ") port: (current-error-port) level: 20)])parameter
Default configuration for the info logger.
- (warning-logger-config [default=(make-logger-config prefix: (lambda () "[WARNING] ") port: (current-error-port) level: 30)])parameter
Default configuration for the warning logger.
- (error-logger-config [default=(make-logger-config prefix: (lambda () "[ERROR] ") port: (current-error-port) level: 40)])parameter
Default configuration for the error logger.
logger-config record constuctor, predicate, getters and setters
- (make-logger-config #!key (prefix "") (port (current-error-port) (level 0)))procedure
Constructor for logger-config objects. Create a logger-config object and return it. Parameters:
- prefix: thunk that returns a string used as a prefix for log lines.
- port (default: (current-error-port)): output port or string representing a path to a file to be used as storage for log messages.
- level: Number representing the log level.
- logger-config? objprocedure
Return #t if obj is a logger-config object; return #f otherwise.
- logger-config-prefix logger-configprocedure
- logger-config-prefix-set! logger-config thunkprocedure
Getter and setter for the prefix attribute of logger-config objects.
- logger-config-port logger-configprocedure
- logger-config-port-set! logger-config port/stringprocedure
Getter and setter for the port attribute of logger-config objects.
- logger-config-level logger-configprocedure
- logger-config-level-set! logger-config numberprocedure
Getter and setter for the level attribute of logger-config objects.
Utilities
- config-logger logger-config #!key prefix port levelprocedure
Helper procedure to create logger-config objects, reusing attributes from a given one. Example:
(define bar (config-logger foo level: 25))
The code above will create a bar logger-config object which will have the same attributes as foo's, but with level 25.
- make-logger config #!optional thunkprocedure
Make a logger procedure using configuration from config (a SRFI-39 parameter). If thunk is provided, a call to it will be injected as the last expression of the returned procedure.
Usage examples
(import (chicken format) (chicken time posix)) (import simple-logger) (log-info "This is the default info logger configuration (won't be printed)") (log-warning "This is the default warning logger configuration") (define (now) (time->string (seconds->local-time) "%Y-%m-%d %H:%M:%S")) ;; Add timestamps to the prefix of error (parameterize ((error-logger-config (config-logger (error-logger-config) prefix: (lambda () (string-append "[ERROR] " (now) " "))))) (log-error "This is error with timestamps")) (log-error "This is the default error logger configuration") ;; Log levels (log-info "This won't be printed") (parameterize ((log-level 0)) (log-info "Now info log is printed")) ;; Making a new logger (define custom-logger-config (make-parameter (make-logger-config prefix: (lambda () "[CUSTOM] ") level: 50))) (define log-custom (make-logger custom-logger-config)) (log-custom "This is the custom logger") ;; A custom debug logger that allows the level to be dynamically set (define custom-debug-logger-config (make-parameter (make-logger-config))) (define debug (let ((logger (make-logger custom-debug-logger-config))) (lambda (level fmt . args) (parameterize ((custom-debug-logger-config (config-logger (custom-debug-logger-config) prefix: (lambda () (sprintf "[DEBUG ~a] " level)) level: (* level 10)))) (apply logger (cons fmt args)))))) (debug 1 "Debug level 1 (won't be printed)") (debug 3 "Debug level 3") (debug 5 "Debug level 5")
If you save the code above into a file and execute it, you'll get something like the output below (the timestamp will most likely be different):
[WARNING] This is the default warning logger configuration [ERROR] 2023-12-27 13:57:00 This is error with timestamps [ERROR] This is the default error logger configuration [INFO] Now info log is printed [CUSTOM] This is the custom logger [DEBUG 3] Debug level 3 [DEBUG 5] Debug level 5
License
Copyright (c) 2023, Mario Domenech Goulart All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Version history
1.0.0 (2023-12-27)
- Initial release