Version 10 of logger

Updated 2006-11-28 16:30:09

logger documentation can be found at http://tcllib.sourceforge.net/doc/logger.html

logger is a tcllib package that attempts to improve on the log package by providing a configurable, hierarchical approach to logging, meaning that one can have not only levels such as critical, error, and warn, but also 'services', such as irc, mime, or whatever one wishes to specify. These services can also have a tree-like structure, so that you could have 'sub' services that are all also configurable via the root of that tree. Furthermore, the code attempts to minimize impact on performance when logging is turned off for a particular service. If all the stops are pulled out, it approaches the speed of not having the logging code at all.

Simple usage example:

 package require logger 0.3

 # initialize logger subsystems
 # two loggers are created
 # 1. main
 # 2. a seperate logger for plugins
 set log [logger::init main]

 namespace eval ::plugin {
     variable name "MyPlugin"
     variable log
     set log [logger::init main::plugins]

     proc pluginlogproc {txt} {
           variable name
           puts stdout "[clock format [clock seconds]] : $name : $txt"
     }

     proc foo {} {
        variable log
        ${log}::notice "A simple message"
     }
 }

 # Testing the logger
 puts "Known log levels: [logger::levels]"
 puts "Known services: [logger::services]" 

 puts "Showing logger configuration"
 ${log}::notice "A simple message from the main logger"
 plugin::foo

 puts "Switching logproc for plugin"
 # change the configuration of the logproc
 ${::plugin::log}::logproc notice ::plugin::pluginlogproc

 ${log}::notice "A simple message from the main logger"
 plugin::foo

 # switching loglevels
 puts "Current loglevel for main: [${log}::currentloglevel]"
 puts "Current loglevel for main::plugin: [${::plugin::log}::currentloglevel]"

 ${::log}::setlevel notice

 puts "Current loglevel for main: [${log}::currentloglevel]"
 puts "Current loglevel for main::plugin: [${::plugin::log}::currentloglevel]"

 ${::plugin::log}::setlevel warn

 puts "Current loglevel for main: [${log}::currentloglevel]"
 puts "Current loglevel for main::plugin: [${::plugin::log}::currentloglevel]"

A basic example of logging to a file:

 package require logger
 proc log_to_file {lvl txt} {
   set logfile "mylog.log"
   set msg "\[[clock format [clock seconds]]\] $txt"
   set f [open $logfile {WRONLY CREAT APPEND}] ;# instead of "a"
   fconfigure $f -encoding utf-8
   puts $f $msg
   close $f
 }

 set log [logger::init global]
 foreach lvl [logger::levels] {
   interp alias {} log_to_file_$lvl {} log_to_file $lvl
   ${log}::logproc $lvl log_to_file_$lvl
 }

 ${log}::info "Logging to a file" 

RLH These are a couple of different time/date formats used in logging:

 # ISO8601 
 # 2006-11-28T11:04:53
 clock format [clock seconds] -format "%Y-%m-%dT%H:%M:%S"

 # APACHE
 # Tue Nov 28 11:14:04 2006
 clock format [clock seconds] -format "%a %b %d %H:%M:%S %Y"


Category Package, a part of Tcllib | Category Debugging | Category File