logging revision dafcb997e390efa4423883dafd100c975c4095d6
7fd0120946822608dcfd6967ce427df472768ed4mikloshCopyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
7fd0120946822608dcfd6967ce427df472768ed4mikloshCopyright (C) 1999-2001 Internet Software Consortium.
7fd0120946822608dcfd6967ce427df472768ed4mikloshSee COPYRIGHT in the source root or http://isc.org/copyright.html for terms.
7fd0120946822608dcfd6967ce427df472768ed4miklosh$Id: logging,v 1.10 2004/03/05 05:04:46 marka Exp $
7fd0120946822608dcfd6967ce427df472768ed4mikloshThe ISC logging system is designed to provide a flexible, extensible
7fd0120946822608dcfd6967ce427df472768ed4mikloshmethod of writing messages. Messages can be sent to the system's
7fd0120946822608dcfd6967ce427df472768ed4mikloshlogging facility, directly to a file, or into the bitbucket, usually
7fd0120946822608dcfd6967ce427df472768ed4mikloshconfigured per the desires of the users of the program. Each message
7fd0120946822608dcfd6967ce427df472768ed4mikloshis associated with a particular category (eg, "security" or
7fd0120946822608dcfd6967ce427df472768ed4miklosh"database") that reflects its nature, and a particular module (such as
7fd0120946822608dcfd6967ce427df472768ed4mikloshthe library's source file) that reflects its origin. Messages are
7fd0120946822608dcfd6967ce427df472768ed4mikloshalso each assigned a priority level which states how remarkable the
7fd0120946822608dcfd6967ce427df472768ed4mikloshmessage is, so that too can be configured by the program's user to
7fd0120946822608dcfd6967ce427df472768ed4mikloshcontrol how much detail is desired.
7fd0120946822608dcfd6967ce427df472768ed4mikloshLibraries which use the ISC logging system can be linked against each
7fd0120946822608dcfd6967ce427df472768ed4mikloshother without fear of conflict. A program is able to select which, if
7fd0120946822608dcfd6967ce427df472768ed4mikloshany, libraries will write log messages.
7fd0120946822608dcfd6967ce427df472768ed4mikloshFUNDAMENTALS
7fd0120946822608dcfd6967ce427df472768ed4mikloshThis section describes the basics of how the system works, introduces
7fd0120946822608dcfd6967ce427df472768ed4mikloshterms and defines C preprocessor symbols used in conjuction with
7fd0120946822608dcfd6967ce427df472768ed4mikloshlogging functions. Actual uses of functions are demonstrated in the
7fd0120946822608dcfd6967ce427df472768ed4mikloshfollowing two sections.
7fd0120946822608dcfd6967ce427df472768ed4mikloshLog messages are associated with three pieces of information that are
7fd0120946822608dcfd6967ce427df472768ed4mikloshused to determine their disposition: a category, a module, and a
7fd0120946822608dcfd6967ce427df472768ed4mikloshlevel (aka "priority").
7fd0120946822608dcfd6967ce427df472768ed4mikloshA category describes the conceptual nature of the message, that is,
7fd0120946822608dcfd6967ce427df472768ed4mikloshwhat general aspect of the code it is concerned with. For example,
7fd0120946822608dcfd6967ce427df472768ed4mikloshthe DNS library defines categories that include the workings of the
7fd0120946822608dcfd6967ce427df472768ed4mikloshdatabase as well security issues. Macros for naming categories are
7fd0120946822608dcfd6967ce427df472768ed4mikloshtypically provided in the library's log header file, such as
7fd0120946822608dcfd6967ce427df472768ed4mikloshDNS_LOGCATEGORY_DATABASE and DNS_LOGCATEGORY_SECURITY in <dns/log.h>
7fd0120946822608dcfd6967ce427df472768ed4mikloshfor the two categories in the previous sentence. The special category
7fd0120946822608dcfd6967ce427df472768ed4mikloshISC_LOGCATEGORY_DEFAULT is associated with any message that does not
7fd0120946822608dcfd6967ce427df472768ed4mikloshmatch a particular category (or matches a category but not a module,
7fd0120946822608dcfd6967ce427df472768ed4mikloshas seen in the next paragraph).
7fd0120946822608dcfd6967ce427df472768ed4mikloshA module is loosely the origin of a message. Though there not be a
7fd0120946822608dcfd6967ce427df472768ed4mikloshone-to-one correspondence of source files with modules, it is typical
7fd0120946822608dcfd6967ce427df472768ed4mikloshthat a module's name reflect the source file in which it is used. So,
7fd0120946822608dcfd6967ce427df472768ed4mikloshfor example, the module identifier DNS_LOGMODULE_RBT would be used by
7fd0120946822608dcfd6967ce427df472768ed4mikloshmessages coming from within the dns/rbt.c source file.
7fd0120946822608dcfd6967ce427df472768ed4mikloshThe specification of the combination of a category and a module for a
7fd0120946822608dcfd6967ce427df472768ed4mikloshmessage are called the message's "category/module pair".
7fd0120946822608dcfd6967ce427df472768ed4mikloshThe level of a message is an indication of its severity. There are
7fd0120946822608dcfd6967ce427df472768ed4mikloshsix standard logging levels, in order here from most to least severe
7fd0120946822608dcfd6967ce427df472768ed4miklosh(least to most common):
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_CRITICAL -- An error so severe it causes the program to exit.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_ERROR -- A very notable error, but the program can go on.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_WARNING -- Something is probably not as it should be.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_NOTICE -- Notable events that occur while the program runs.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_INFO -- Statistics, typically.
7fd0120946822608dcfd6967ce427df472768ed4mikloshand finally:
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_DEBUG(unsigned int level) -- detailed debugging messages.
7fd0120946822608dcfd6967ce427df472768ed4mikloshISC_LOG_DEBUG is not quite like the others in that it takes an
7fd0120946822608dcfd6967ce427df472768ed4mikloshargument the defines roughly how detailed the message is; a higher
7fd0120946822608dcfd6967ce427df472768ed4mikloshlevel means more copious detail, so that values near 0 would be used
7fd0120946822608dcfd6967ce427df472768ed4mikloshat places like the entry to major sections of code, while greater
7fd0120946822608dcfd6967ce427df472768ed4mikloshnumbers would be used inside loops.
7fd0120946822608dcfd6967ce427df472768ed4mikloshSo, ok, technically there are five + at least 4,294,967,296 levels.
7fd0120946822608dcfd6967ce427df472768ed4mikloshPicky picky. In any event, the six levels correspond with similar
7fd0120946822608dcfd6967ce427df472768ed4mikloshlevels used by Unix's syslog, and when messages using one of those
7fd0120946822608dcfd6967ce427df472768ed4mikloshlevels is sent to syslog, the equivalent syslog level is used. (Note
7fd0120946822608dcfd6967ce427df472768ed4mikloshthat this means that any debugging messages go to the singular
7fd0120946822608dcfd6967ce427df472768ed4mikloshLOG_DEBUG priority in syslog, regardless of their level internal to
7fd0120946822608dcfd6967ce427df472768ed4mikloshthe ISC logging system.)
7fd0120946822608dcfd6967ce427df472768ed4mikloshThe next building block of the logging system is a channel. A channel
7fd0120946822608dcfd6967ce427df472768ed4mikloshspecifies where a message of a particular priority level should go, as
7fd0120946822608dcfd6967ce427df472768ed4mikloshwell as any special options for that destination. There are four
7fd0120946822608dcfd6967ce427df472768ed4mikloshbasic destinations, as follows:
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_TOSYSLOG -- Send it to syslog.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_TOFILE -- Write to a file.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_TOFILEDESC -- Write to a (previously opened) file descriptor.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_TONULL -- Do not write the message when selected.
7fd0120946822608dcfd6967ce427df472768ed4mikloshA file destination names a path to a log file. It also specifies the
7fd0120946822608dcfd6967ce427df472768ed4mikloshmaximum allowable byte size of the file before it is closed (where 0
7fd0120946822608dcfd6967ce427df472768ed4mikloshmeans no limit) and the number of versions of a file to keep (where
7fd0120946822608dcfd6967ce427df472768ed4mikloshISC_LOG_ROLLNEVER means the logging system never renames the log file,
7fd0120946822608dcfd6967ce427df472768ed4mikloshand ISC_LOG_ROLLINFINITE means no cap on the number of versions).
7fd0120946822608dcfd6967ce427df472768ed4mikloshVersion control is done just before a file is opened, so a program
7fd0120946822608dcfd6967ce427df472768ed4mikloshthat used it would start with a fresh log file (unless using
7fd0120946822608dcfd6967ce427df472768ed4mikloshISC_LOG_ROLLNEVER) each time it ran. If you want to use an external
7fd0120946822608dcfd6967ce427df472768ed4mikloshrolling method, use ISC_LOG_ROLLNEVER and ensure that your program has
7fd0120946822608dcfd6967ce427df472768ed4miklosha mechanism for calling isc_log_closefilelogs().
7fd0120946822608dcfd6967ce427df472768ed4miklosh(ISC_LOG_ROLLINFINITE is not truly infinite; it will stop at INT_MAX.
7fd0120946822608dcfd6967ce427df472768ed4mikloshOn 32 bit machines that means the logs would need to roll once per
7fd0120946822608dcfd6967ce427df472768ed4mikloshsecond for more than sixty years before exhausting the version number
7fd0120946822608dcfd6967ce427df472768ed4mikloshA file descriptor destination is simply associated with a previously
7fd0120946822608dcfd6967ce427df472768ed4mikloshopened stdio file descriptor. This is mostly used for associating
7fd0120946822608dcfd6967ce427df472768ed4mikloshstdout or stderr with log messages, but could also be used, for
7fd0120946822608dcfd6967ce427df472768ed4mikloshexample, to send logging messages down a pipe that has been opened by
7fd0120946822608dcfd6967ce427df472768ed4mikloshthe program. File descriptor destinations are never closed, have no
7fd0120946822608dcfd6967ce427df472768ed4mikloshmaximum size limit, and do not do version control.
7fd0120946822608dcfd6967ce427df472768ed4mikloshSyslog destinations are associated with the standard syslog facilities
7fd0120946822608dcfd6967ce427df472768ed4mikloshavailable on your system. They too have no maximum size limit and do
7fd0120946822608dcfd6967ce427df472768ed4mikloshno version control.
7fd0120946822608dcfd6967ce427df472768ed4mikloshSince null channels go nowhere, no additional destination
7fd0120946822608dcfd6967ce427df472768ed4mikloshspecification is necessary.
7fd0120946822608dcfd6967ce427df472768ed4mikloshThe words "destination" and "channel" can be used interchangably in
7fd0120946822608dcfd6967ce427df472768ed4mikloshsome contexts. Referring to a file channel, for example, means a
7fd0120946822608dcfd6967ce427df472768ed4mikloshchannel that has a file destination.
7fd0120946822608dcfd6967ce427df472768ed4mikloshChannels have string names that are their primary external reference.
7fd0120946822608dcfd6967ce427df472768ed4mikloshThere are four predefined logging channels:
7fd0120946822608dcfd6967ce427df472768ed4miklosh "default_stderr" -- Descriptor channel to stderr at priority ISC_LOG_INFO
7fd0120946822608dcfd6967ce427df472768ed4miklosh "default_debug" -- Descriptor channel to stderr at priority ISC_LOG_DYNAMIC
7fd0120946822608dcfd6967ce427df472768ed4miklosh "default_syslog" -- Syslog channel to LOG_DAEMON at priority ISC_LOG_INFO
7fd0120946822608dcfd6967ce427df472768ed4miklosh "null" -- Null channel
7fd0120946822608dcfd6967ce427df472768ed4mikloshWhat's ISC_LOG_DYNAMIC? That's how you tell the logging system that
7fd0120946822608dcfd6967ce427df472768ed4mikloshyou want debugging messages, but only at the current debugging level
7fd0120946822608dcfd6967ce427df472768ed4mikloshof the program. The debugging level is controlled as described near
7fd0120946822608dcfd6967ce427df472768ed4mikloshthe end of the next section. When the debugging level is 0 (turned
7fd0120946822608dcfd6967ce427df472768ed4mikloshoff), then no debugging messages are written to the channel. If the
7fd0120946822608dcfd6967ce427df472768ed4mikloshdebugging level is raised, only debugging messages up to its level are
7fd0120946822608dcfd6967ce427df472768ed4mikloshwritten to the channel.
7fd0120946822608dcfd6967ce427df472768ed4mikloshYou can reuse a channel name. If you define a channel with the same
7fd0120946822608dcfd6967ce427df472768ed4mikloshname as an existing channel, the new definition is used by all future
7fd0120946822608dcfd6967ce427df472768ed4mikloshreferences to the name. The old definition is still used by anything
7fd0120946822608dcfd6967ce427df472768ed4mikloshthat was pointing to the name before the redefinition. This even
7fd0120946822608dcfd6967ce427df472768ed4mikloshapplies to redefinitions of the predefined channels, with one
7fd0120946822608dcfd6967ce427df472768ed4mikloshexception: redefining default_stderr will change the default
7fd0120946822608dcfd6967ce427df472768ed4mikloshdestination of messages, as explained in more detail in a few paragraphs.
7fd0120946822608dcfd6967ce427df472768ed4mikloshChannels can additionally have any of five options associated with
7fd0120946822608dcfd6967ce427df472768ed4mikloshthem. The following options are listed in the order which their
7fd0120946822608dcfd6967ce427df472768ed4mikloshcorresponding print strings appear in a log message:
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_PRINTTIME -- The date and time.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_PRINTCATEGORY -- The category name.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_PRINTMODULE -- The module name.
7fd0120946822608dcfd6967ce427df472768ed4miklosh ISC_LOG_PRINTLEVEL -- The level.
7fd0120946822608dcfd6967ce427df472768ed4mikloshYou can set all four of those options with ISC_LOG_PRINTALL.
7fd0120946822608dcfd6967ce427df472768ed4mikloshSyslog channels do not need ISC_LOG_PRINTTIME, but it is usally a good
7fd0120946822608dcfd6967ce427df472768ed4mikloshidea for file and file descriptor feeds.
7fd0120946822608dcfd6967ce427df472768ed4mikloshThe additional option does not affect formatting. It is
7fd0120946822608dcfd6967ce427df472768ed4mikloshISC_LOG_DEBUGONLY, and marks a channel as only being eligible for
7fd0120946822608dcfd6967ce427df472768ed4mikloshmessages when the debugging level is non-zero. It acts like the
7fd0120946822608dcfd6967ce427df472768ed4mikloshnull channel when the debugging level is zero.
7fd0120946822608dcfd6967ce427df472768ed4mikloshNow with these objects -- the category, module, and channel -- you can
7fd0120946822608dcfd6967ce427df472768ed4mikloshactually direct messages to your desired destinations. As shown in
7fd0120946822608dcfd6967ce427df472768ed4mikloshthe next section, you associate the category/module pair with a
7fd0120946822608dcfd6967ce427df472768ed4mikloshchannel. It is possible to use one function call to say "all modules
7fd0120946822608dcfd6967ce427df472768ed4mikloshcoupled with this category" and vice versa, but conceptually the
7fd0120946822608dcfd6967ce427df472768ed4mikloshmatching is still referred to as applying to category/module pairs,
7fd0120946822608dcfd6967ce427df472768ed4mikloshsince that is what comes in from functions writing messages.
7fd0120946822608dcfd6967ce427df472768ed4mikloshSpeaking of functions writing messages, here's what happens when a
7fd0120946822608dcfd6967ce427df472768ed4mikloshfunction wants to write a message through the logging system. First
7fd0120946822608dcfd6967ce427df472768ed4mikloshthe function calls isc_log_write(), specifying a category, module and
7fd0120946822608dcfd6967ce427df472768ed4mikloshIn isc_log_write(), the logging system first looks up a list that
7fd0120946822608dcfd6967ce427df472768ed4mikloshconsists of all of the channels associated with a particular category.
7fd0120946822608dcfd6967ce427df472768ed4mikloshIt walks down the list looking for each channel that also has the
7fd0120946822608dcfd6967ce427df472768ed4mikloshindicated module associated with it, and writes the message to each
7fd0120946822608dcfd6967ce427df472768ed4mikloshchannel it encounters. If no match is found in the list for the
7fd0120946822608dcfd6967ce427df472768ed4mikloshmodule, the default channel is used. Similarly, the default is used
7fd0120946822608dcfd6967ce427df472768ed4mikloshif no channels have been specified for the category at all.
7fd0120946822608dcfd6967ce427df472768ed4mikloshWhat is the default? It is ISC_LOGCATEGORY_DEFAULT -- sort of. You
7fd0120946822608dcfd6967ce427df472768ed4mikloshcan specify an association of the channel ISC_LOGCATEGORY_DEFAULT with
7fd0120946822608dcfd6967ce427df472768ed4mikloshany particular module, or more usually all of them, and that's what
7fd0120946822608dcfd6967ce427df472768ed4mikloshwill be used for any category/module pair for which you have not
7fd0120946822608dcfd6967ce427df472768ed4mikloshspecified a channel. If you do not associate ISC_LOGCATGORY_DEFAULT
7fd0120946822608dcfd6967ce427df472768ed4mikloshand the indicated module, then the internal default of using the
7fd0120946822608dcfd6967ce427df472768ed4mikloshdefault_stderr channel is used. This brings us back to the statement
7fd0120946822608dcfd6967ce427df472768ed4mikloshmade a few paragraphs ago about redefining the predefined channels --
7fd0120946822608dcfd6967ce427df472768ed4mikloshif you redefine default_stderr, and a messages comes in for a
7fd0120946822608dcfd6967ce427df472768ed4mikloshcategory/module pair that has had neither its original pair or
7fd0120946822608dcfd6967ce427df472768ed4mikloshthe ISC_LOGCATEGORY_DEFAULT/module pair configured for it, then the
Here are some other ways to think about how category/module pairs get
The default is only used when a category/module pair has not been
category/module pair.
category/module pair has no effect on any other destinations
category/module pair, but the exact mechanism has not yet been
#include <isc/log.h>
#include <dns/log.h>
destination.file.maximum_size = 0; /* No byte limit. */
destination.file.versions = ISC_LOG_ROLLNEVER; /* External rolling. */
destination.file.stream = stdout;
destination.facility = LOG_ERR;
This section describes how a new library, libfoo.a, would use the ISC
* includes isc/log.h
See <dns/log.h> for a sample.
2) Write a C source module that includes the library's log.h,
#include <isc/result.h>
#include <isc/log.h>
#include <dns/log.h>
I am not happy that using a null channel for a category/module pair
category/module, so that internally when walking down the channel
list, the first category/module match to a null channel stops