Wiki
Download
Manual
Eggs
API
Tests
Bugs
show
edit
history
You can edit this page using
wiki syntax
for markup.
Article contents:
== simple-logger [[toc:]] === Author Mario Domenech Goulart === Repository [[https://github.com/mario-goulart/simple-logger|https://github.com/mario-goulart/simple-logger]] === Requirements None === Description A very simple logger for CHICKEN. === API ==== Logging procedures <procedure>(log-info fmt . args)</procedure> 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: <enscript highlight=scheme> (log-info "hey") (let ((some-variable "blah")) (log-info "The value of some-variable is ~a" some-variable)) </enscript> <procedure>(log-warning fmt . args)</procedure> 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: <enscript highlight=scheme> (log-warning "You've been warned!") (let ((some-variable "blah")) (log-warning "The value of some-variable is ~a" some-variable)) </enscript> <procedure>(log-error fmt . args)</procedure> 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: <enscript highlight=scheme> (log-error "KABOOM!") (let ((some-variable "blah")) (log-error "The value of some-variable is ~a" some-variable)) </enscript> <procedure>(log-debug fmt . args)</procedure> 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: <enscript highlight=scheme> (log-debug "Here!") (let ((some-variable "blah")) (log-debug "The value of some-variable is ~a" some-variable)) </enscript> <procedure>(die! fmt . args)</procedure> 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: <enscript highlight=scheme> (die! "Goodbye, cruel world.") </enscript> ==== Configuration parameters <parameter>(log-level [default=30])</parameter> 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)])</parameter> Default configuration for the {{debug}} logger. <parameter>(info-logger-config [default=(make-logger-config prefix: (lambda () "[INFO] ") port: (current-error-port) level: 20)])</parameter> Default configuration for the {{info}} logger. <parameter>(warning-logger-config [default=(make-logger-config prefix: (lambda () "[WARNING] ") port: (current-error-port) level: 30)])</parameter> Default configuration for the warning logger. <parameter>(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 <procedure>(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. <procedure>(logger-config? obj)</procedure> Return {{#t}} if {{obj}} is a {{logger-config}} object; return {{#f}} otherwise. <procedure>(logger-config-prefix logger-config)</procedure> <procedure>(logger-config-prefix-set! logger-config thunk)</procedure> Getter and setter for the {{prefix}} attribute of {{logger-config}} objects. <procedure>(logger-config-port logger-config)</procedure> <procedure>(logger-config-port-set! logger-config port/string)</procedure> Getter and setter for the {{port}} attribute of {{logger-config}} objects. <procedure>(logger-config-level logger-config)</procedure> <procedure>(logger-config-level-set! logger-config number)</procedure> Getter and setter for the {{level}} attribute of {{logger-config}} objects. ==== Utilities <procedure>(config-logger logger-config #!key prefix port level)</procedure> 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)</procedure> 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 <enscript highlight=scheme> (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") </enscript> 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
Description of your changes:
I would like to authenticate
Authentication
Username:
Password:
Spam control
What do you get when you add 8 to 5?