Avoid redundant YAZ includes.
[yaz4j-moved-to-github.git] / dependencies / yaz-2.1.28 / doc / tools.log.html
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>4. Log</title><meta name="generator" content="DocBook XSL Stylesheets V1.70.1"><link rel="start" href="index.html" title="YAZ User's Guide and Reference"><link rel="up" href="tools.html" title="Chapter 8. Supporting Tools"><link rel="prev" href="tools.nmem.html" title="3. Nibble Memory"><link rel="next" href="tools.marc.html" title="5. MARC"></head><body><link rel="stylesheet" type="text/css" href="common/style1.css"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">4. Log</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="tools.nmem.html">Prev</a> </td><th width="60%" align="center">Chapter 8. Supporting Tools</th><td width="20%" align="right"> <a accesskey="n" href="tools.marc.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="tools.log"></a>4. Log</h2></div></div></div><p>
2    YAZ has evolved a fairly complex log system which should be useful both 
3    for debugging YAZ itself, debugging applications that use YAZ, and for
4    production use of those applications.  
5   </p><p>
6    The log functions are declared in header <code class="filename">yaz/log.h</code>
7     and implemented in <code class="filename">src/log.c</code>.
8     Due to name clash with syslog and some math utilities the logging
9     interface has been modified as of YAZ 2.0.29. The obsolete interface
10     is still available if in header file <code class="filename">yaz/log.h</code>.
11     The key points of the interface are:
12   </p><pre class="screen">
13    void yaz_log(int level, const char *fmt, ...)
14
15    void yaz_log_init(int level, const char *prefix, const char *name);
16    void yaz_log_init_file(const char *fname);
17    void yaz_log_init_level(int level);
18    void yaz_log_init_prefix(const char *prefix);
19    void yaz_log_time_format(const char *fmt);
20    void yaz_log_init_max_size(int mx);
21
22    int yaz_log_mask_str(const char *str);
23    int yaz_log_module_level(const char *name);
24   </pre><p>
25    The reason for the whole log module is the <code class="function">yaz_log</code>
26    function. It takes a bitmask indicating the log levels, a
27    <code class="literal">printf</code>-like format string, and a variable number of
28    arguments to log.
29   </p><p>
30    The <code class="literal">log level</code> is a bit mask, that says on which level(s)
31    the log entry should be made, and optionally set some behaviour of the
32    logging. In the most simple cases, it can be one of <code class="literal">YLOG_FATAL,
33    YLOG_DEBUG, YLOG_WARN, YLOG_LOG</code>. Those can be combined with bits
34    that modify the way the log entry is written:<code class="literal">YLOG_ERRNO,
35    YLOG_NOTIME, YLOG_FLUSH</code>.
36    Most of the rest of the bits are deprecated, and should not be used. Use
37    the dynamic log levels instead.
38   </p><p>
39    Applications that use YAZ, should not use the LOG_LOG for ordinary
40    messages, but should make use of the dynamic loglevel system. This consists
41    of two parts, defining the loglevel and checking it.
42   </p><p>
43    To define the log levels, the (main) program should pass a string to
44    <code class="function">yaz_log_mask_str</code> to define which log levels are to be
45    logged. This string should be a comma-separated list of log level names,
46    and can contain both hard-coded names and dynamic ones. The log level
47    calculation starts with <code class="literal">YLOG_DEFAULT_LEVEL</code> and adds a bit
48    for each word it meets, unless the word starts with a '-', in which case it 
49    clears the bit. If the string <code class="literal">'none'</code> is found,
50    all bits are cleared. Typically this string comes from the command-line,
51    often identified by <code class="literal">-v</code>. The
52    <code class="function">yaz_log_mask_str</code> returns a log level that should be
53    passed to <code class="function">yaz_log_init_level</code> for it to take effect.
54   </p><p>
55    Each module should check what log bits it should be used, by calling 
56    <code class="function">yaz_log_module_level</code> with a suitable name for the
57    module. The name is cleared from a preceding path and an extension, if any,
58    so it is quite possible to use <code class="literal">__FILE__</code> for it. If the
59    name has been passed to <code class="function">yaz_log_mask_str</code>, the routine
60    returns a non-zero bitmask, which should then be used in consequent calls
61    to yaz_log. (It can also be tested, so as to avoid unnecessary calls to
62    yaz_log, in time-critical places, or when the log entry would take time 
63    to construct.) 
64   </p><p>
65    Yaz uses the following dynamic log levels:
66    <code class="literal">server, session, request, requestdetail</code> for the server
67    functionality.
68    <code class="literal">zoom</code> for the zoom client api.
69    <code class="literal">ztest</code> for the simple test server.
70    <code class="literal">malloc, nmem, odr, eventl</code> for internal debugging of yaz itself.
71    Of course, any program using yaz is welcome to define as many new ones, as
72    it needs.
73   </p><p>
74    By default the log is written to stderr, but this can be changed by a call
75    to <code class="function">yaz_log_init_file</code> or
76    <code class="function">yaz_log_init</code>. If the log is directed to a file, the
77    file size is checked at every write, and if it exceeds the limit given in
78    <code class="function">yaz_log_init_max_size</code>, the log is rotated. The
79    rotation keeps one old version (with a <code class="literal">.1</code> appended to
80    the name). The size defaults to 1GB. Setting it to zero will disable the
81    rotation feature.
82   </p><pre class="screen">
83   A typical yaz-log looks like this
84   13:23:14-23/11 yaz-ztest(1) [session] Starting session from tcp:127.0.0.1 (pid=30968)
85   13:23:14-23/11 yaz-ztest(1) [request] Init from 'YAZ' (81) (ver 2.0.28) OK
86   13:23:17-23/11 yaz-ztest(1) [request] Search Z: @attrset Bib-1 foo  OK:7 hits
87   13:23:22-23/11 yaz-ztest(1) [request] Present: [1] 2+2  OK 2 records returned
88   13:24:13-23/11 yaz-ztest(1) [request] Close OK
89   </pre><p>
90    The log entries start with a time stamp. This can be omitted by setting the
91    <code class="literal">YLOG_NOTIME</code> bit in the loglevel. This way automatic tests
92    can be hoped to produce identical log files, that are easy to diff. The
93    format of the time stamp can be set with
94    <code class="function">yaz_log_time_format</code>, which takes a format string just
95    like <code class="function">strftime</code>.
96   </p><p>
97    Next in a log line comes the prefix, often the name of the program. For
98    yaz-based servers, it can also contain the session number. Then
99    comes one or more logbits in square brackets, depending on the logging
100    level set by <code class="function">yaz_log_init_level</code> and the loglevel
101    passed to <code class="function">yaz_log_init_level</code>. Finally comes the format
102    string and additional values passed to <code class="function">yaz_log</code>
103   </p><p>
104    The log level <code class="literal">YLOG_LOGLVL</code>, enabled by the string
105    <code class="literal">loglevel</code>, will log all the log-level affecting
106    operations. This can come in handy if you need to know what other log
107    levels would be useful. Grep the logfile for <code class="literal">[loglevel]</code>.
108   </p><p>
109    The log system is almost independent of the rest of YAZ, the only
110    important dependence is of <code class="filename">nmem</code>, and that only for
111    using the semaphore definition there. 
112   </p><p>
113    The dynamic log levels and log rotation were introduced in YAZ 2.0.28. At
114    the same time, the log bit names were changed from
115    <code class="literal">LOG_something</code> to <code class="literal">YLOG_something</code>, 
116    to avoid collision with <code class="filename">syslog.h</code>.
117   </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tools.nmem.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tools.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tools.marc.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3. Nibble Memory </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5. MARC</td></tr></table></div></body></html>