Tcl Library Source Code

Bounty program for improvements to Tcl and certain Tcl packages.

[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]


log - Procedures to log messages of libraries and applications.

Table Of Contents


package require Tcl 8
package require log ?1.4?

::log::lv2longform level
::log::lv2color level
::log::lv2priority level
::log::lv2cmd level
::log::lv2channel level
::log::lvCompare level1 level2
::log::lvSuppress level {suppress 1}
::log::lvSuppressLE level {suppress 1}
::log::lvIsSuppressed level
::log::lvCmd level cmd
::log::lvCmdForall cmd
::log::lvChannel level chan
::log::lvChannelForall chan
::log::lvColor level color
::log::lvColorForall color
::log::log level text
::log::logarray level arrayvar ?pattern?
::log::loghex level text data
::log::logsubst level msg
::log::logMsg text
::log::logError text
::log::Puts level text


The log package provides commands that allow libraries and applications to selectively log information about their internal operation and state.

To use the package just execute

package require log
log::log notice "Some message"

As can be seen above, each message given to the log facility is associated with a level determining the importance of the message. The user can then select which levels to log, what commands to use for the logging of each level and the channel to write the message to. In the following example the logging of all message with level debug is deactivated.

package require log
log::lvSuppress debug
log::log debug "Unseen message" ; # No output

By default all messages associated with an error-level (emergency, alert, critical, and error) are written to stderr. Messages with any other level are written to stdout. In the following example the log module is reconfigured to write debug messages to stderr too.

package require log
log::lvChannel debug stderr
log::log debug "Written to stderr"

Each message level is also associated with a command to use when logging a message with that level. The behaviour above for example relies on the fact that all message levels use by default the standard command ::log::Puts to log any message. In the following example all messages of level notice are given to the non-standard command toText for logging. This disables the channel setting for such messages, assuming that toText does not use it by itself.

package require log
log::lvCmd notice toText
log::log notice "Handled by \"toText\""

Another database maintained by this facility is a map from message levels to colors. The information in this database has no influence on the behaviour of the module. It is merely provided as a convenience and in anticipation of the usage of this facility in tk-based application which may want to colorize message logs.


The following commands are available:


The package currently defines the following log levels, the level of highest importance listed first.

Note that by default all messages with levels warning down to debug are suppressed. This is done intentionally, because (we believe that) in most situations debugging output is not wanted. Most people wish to have such output only when actually debugging an application.

Bugs, Ideas, Feedback

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category log of the Tcllib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.


log, log level, message, message level


Programming tools


Copyright © 2001-2009 Andreas Kupries