simple-logger

  1. simple-logger
    1. Author
    2. Repository
    3. Requirements
    4. Description
    5. API
      1. Logging procedures
      2. Configuration parameters
      3. logger-config record constuctor, predicate, getters and setters
      4. Utilities
    6. Usage examples
    7. License
    8. Version history
      1. 1.0.0 (2023-12-27)

Author

Mario Domenech Goulart

Repository

https://github.com/mario-goulart/simple-logger

Requirements

None

Description

A very simple logger for CHICKEN.

API

Logging procedures

[procedure] (log-info fmt . args)

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))
[procedure] (log-warning fmt . args)

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))
[procedure] (log-error fmt . args)

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))
[procedure] (log-debug fmt . args)

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))
[procedure] (die! fmt . args)

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

[parameter] (log-level [default=30])

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.

[parameter] (debug-logger-config [default=(make-logger-config prefix: (lambda () "[DEBUG] ") port: (current-error-port) level: 10)])

Default configuration for the debug logger.

[parameter] (info-logger-config [default=(make-logger-config prefix: (lambda () "[INFO] ") port: (current-error-port) level: 20)])

Default configuration for the info logger.

[parameter] (warning-logger-config [default=(make-logger-config prefix: (lambda () "[WARNING] ") port: (current-error-port) level: 30)])

Default configuration for the warning logger.

[parameter] (error-logger-config [default=(make-logger-config prefix: (lambda () "[ERROR] ") port: (current-error-port) level: 40)])

Default configuration for the error logger.

logger-config record constuctor, predicate, getters and setters

[procedure] (make-logger-config #!key (prefix "") (port (current-error-port) (level 0)))

Constructor for logger-config objects. Create a logger-config object and return it. Parameters:

[procedure] (logger-config? obj)

Return #t if obj is a logger-config object; return #f otherwise.

[procedure] (logger-config-prefix logger-config)
[procedure] (logger-config-prefix-set! logger-config thunk)

Getter and setter for the prefix attribute of logger-config objects.

[procedure] (logger-config-port logger-config)
[procedure] (logger-config-port-set! logger-config port/string)

Getter and setter for the port attribute of logger-config objects.

[procedure] (logger-config-level logger-config)
[procedure] (logger-config-level-set! logger-config number)

Getter and setter for the level attribute of logger-config objects.

Utilities

[procedure] (config-logger logger-config #!key prefix port level)

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.

[procedure] (make-logger config #!optional thunk)

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)