logger_manager_impl.cc 8.59 KB
Newer Older
Stephen Morris's avatar
Stephen Morris committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15
#include <algorithm>
Stephen Morris's avatar
Stephen Morris committed
16
#include <iostream>
17

Stephen Morris's avatar
Stephen Morris committed
18
19
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
20
#include <log4cplus/hierarchy.h>
Stephen Morris's avatar
Stephen Morris committed
21
#include <log4cplus/consoleappender.h>
Stephen Morris's avatar
Stephen Morris committed
22
#include <log4cplus/fileappender.h>
23
#include <log4cplus/syslogappender.h>
Stephen Morris's avatar
Stephen Morris committed
24

25
#include <log/logger.h>
Jelte Jansen's avatar
Jelte Jansen committed
26
#include <log/logger_support.h>
27
28
29
30
31
32
#include <log/logger_level_impl.h>
#include <log/logger_manager.h>
#include <log/logger_manager_impl.h>
#include <log/log_messages.h>
#include <log/logger_name.h>
#include <log/logger_specification.h>
33
#include <log/log_buffer.h>
34

Jelte Jansen's avatar
Jelte Jansen committed
35
36
#include <boost/scoped_ptr.hpp>

37
38
using namespace std;

Stephen Morris's avatar
Stephen Morris committed
39
40
41
namespace isc {
namespace log {

Stephen Morris's avatar
Stephen Morris committed
42
43
44
45
46
// Reset hierarchy of loggers back to default settings.  This removes all
// appenders from loggers, sets their severity to NOT_SET (so that events are
// passed back to the parent) and resets the root logger to logging
// informational messages.  (This last is not a log4cplus default, so we have to
// explicitly reset the logging severity.)
Stephen Morris's avatar
Stephen Morris committed
47
void
48
LoggerManagerImpl::processInit() {
Stephen Morris's avatar
Stephen Morris committed
49
    log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
50
    initRootLogger();
Stephen Morris's avatar
Stephen Morris committed
51
52
53
54
55
56
}

// Process logging specification.  Set up the common states then dispatch to
// add output specifications.
void
LoggerManagerImpl::processSpecification(const LoggerSpecification& spec) {
Jelte Jansen's avatar
Jelte Jansen committed
57
58
59
60
61
62
    log4cplus::Logger logger;
    // If this is an 'empty' specification, just set the root logger
    if (spec.getName() == "") {
        logger = log4cplus::Logger::getInstance(getRootLoggerName());
    } else {
        logger = log4cplus::Logger::getInstance(
63
                                   expandLoggerName(spec.getName()));
Jelte Jansen's avatar
Jelte Jansen committed
64
    }
Stephen Morris's avatar
Stephen Morris committed
65
66
67
68
69
70
71
72

    // Set severity level according to specification entry.
    logger.setLogLevel(LoggerLevelImpl::convertFromBindLevel(
                       Level(spec.getSeverity(), spec.getDbglevel())));

    // Set the additive flag.
    logger.setAdditivity(spec.getAdditive());

73
74
    // Output options given?
    if (spec.optionCount() > 0) {
75
76
        // Replace all appenders for this logger.
        logger.removeAllAppenders();
77
78
79
80
81
82
83
84
85
86
87
88
89
90

        // Now process output specifications.
        for (LoggerSpecification::const_iterator i = spec.begin();
             i != spec.end(); ++i) {
            switch (i->destination) {
            case OutputOption::DEST_CONSOLE:
                createConsoleAppender(logger, *i);
                break;

            case OutputOption::DEST_FILE:
                createFileAppender(logger, *i);
                break;

            case OutputOption::DEST_SYSLOG:
91
                createSyslogAppender(logger, *i);
92
93
94
                break;

            default:
95
96
97
98
                // Not a valid destination.  As we are in the middle of updating
                // logging destinations, we could be in the situation where
                // there are no valid appenders.  For this reason, throw an
                // exception.
99
100
101
102
                isc_throw(UnknownLoggingDestination,
                          "Unknown logging destination, code = " <<
                          i->destination);
            }
Stephen Morris's avatar
Stephen Morris committed
103
104
        }
    }
Jelte Jansen's avatar
Jelte Jansen committed
105
106
    // Should anything be left in the buffer, this is the time to flush it.
    getLogBuffer().flush();
Stephen Morris's avatar
Stephen Morris committed
107
108
109
110
111
112
113
114
115
116
}

// Console appender - log to either stdout or stderr.
void
LoggerManagerImpl::createConsoleAppender(log4cplus::Logger& logger,
                                         const OutputOption& opt)
{
    log4cplus::SharedAppenderPtr console(
        new log4cplus::ConsoleAppender(
            (opt.stream == OutputOption::STR_STDERR), opt.flush));
117
    setConsoleAppenderLayout(console);
Stephen Morris's avatar
Stephen Morris committed
118
119
120
    logger.addAppender(console);
}

Stephen Morris's avatar
Stephen Morris committed
121
122
123
124
125
126
// File appender.  Depending on whether a maximum size is given, either
// a standard file appender or a rolling file appender will be created.
void
LoggerManagerImpl::createFileAppender(log4cplus::Logger& logger,
                                         const OutputOption& opt)
{
127
128
    // Append to existing file
    std::ios::openmode mode = std::ios::app;
Stephen Morris's avatar
Stephen Morris committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

    log4cplus::SharedAppenderPtr fileapp;
    if (opt.maxsize == 0) {
        fileapp = log4cplus::SharedAppenderPtr(new log4cplus::FileAppender(
            opt.filename, mode, opt.flush));
    } else {
        fileapp = log4cplus::SharedAppenderPtr(
            new log4cplus::RollingFileAppender(opt.filename, opt.maxsize,
                                               opt.maxver, opt.flush));
    }

    // use the same console layout for the files.
    setConsoleAppenderLayout(fileapp);
    logger.addAppender(fileapp);
}

Jelte Jansen's avatar
Jelte Jansen committed
145
void
146
LoggerManagerImpl::createBufferAppender(log4cplus::Logger& logger) {
147
    log4cplus::SharedAppenderPtr bufferapp(new BufferAppender(getLogBuffer()));
Jelte Jansen's avatar
Jelte Jansen committed
148
149
    bufferapp->setName("buffer");
    logger.addAppender(bufferapp);
150
151
152
    // Since we do not know at what level the loggers will end up
    // running, set it to the highest for now
    logger.setLogLevel(log4cplus::TRACE_LOG_LEVEL);
Jelte Jansen's avatar
Jelte Jansen committed
153
154
155
}

// Syslog appender.
156
void
157
LoggerManagerImpl::createSyslogAppender(log4cplus::Logger& logger,
158
159
160
161
                                         const OutputOption& opt)
{
    log4cplus::SharedAppenderPtr syslogapp(
        new log4cplus::SysLogAppender(opt.facility));
162
    setSyslogAppenderLayout(syslogapp);
163
164
165
    logger.addAppender(syslogapp);
}

Stephen Morris's avatar
Stephen Morris committed
166

167
168
// One-time initialization of the log4cplus system
void
169
170
171
LoggerManagerImpl::init(isc::log::Severity severity, int dbglevel,
                        bool buffer)
{
172
173
174
175
176
177
178
179
180
181
    // Set up basic configurator.  This attaches a ConsoleAppender to the
    // root logger with suitable output.  This is used until we we have
    // actually read the logging configuration, in which case the output
    // may well be changed.
    log4cplus::BasicConfigurator config;
    config.configure();

    // Add the additional debug levels
    LoggerLevelImpl::init();

182
    initRootLogger(severity, dbglevel, buffer);
Stephen Morris's avatar
Stephen Morris committed
183
184
185
186
187
188
}

// Reset logging to default configuration.  This closes all appenders
// and resets the root logger to output INFO messages to the console.
// It is principally used in testing.
void
189
LoggerManagerImpl::reset(isc::log::Severity severity, int dbglevel)
190
{
Stephen Morris's avatar
Stephen Morris committed
191
    // Initialize the root logger
192
    initRootLogger(severity, dbglevel);
193
194
195
}

// Initialize the root logger
196
void LoggerManagerImpl::initRootLogger(isc::log::Severity severity,
197
                                       int dbglevel, bool buffer)
Stephen Morris's avatar
Stephen Morris committed
198
199
{
    log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
200

201
202
203
    // Set the log4cplus root to not output anything - effectively we are
    // ignoring it.
    log4cplus::Logger::getRoot().setLogLevel(log4cplus::OFF_LOG_LEVEL);
204

205
206
207
208
209
210
211
    // Set the level for the BIND 10 root logger to the given severity and
    // debug level.
    log4cplus::Logger b10root = log4cplus::Logger::getInstance(
                                                    getRootLoggerName());
    b10root.setLogLevel(LoggerLevelImpl::convertFromBindLevel(
                                                    Level(severity, dbglevel)));

212
213
214
215
216
217
    if (buffer) {
        createBufferAppender(b10root);
    } else {
        OutputOption opt;
        createConsoleAppender(b10root, opt);
    }
218
219
220
}

void LoggerManagerImpl::setConsoleAppenderLayout(
221
        log4cplus::SharedAppenderPtr& appender)
222
223
{
    // Create the pattern we want for the output - local time.
224
    string pattern = "%D{%Y-%m-%d %H:%M:%S.%q} %-5p [%c/%i] %m\n";
225
226
227
228
229
230

    // Finally the text of the message
    auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(pattern));
    appender->setLayout(layout);
}

231
232
233
234
// Set the the "syslog" layout for the given appenders.  This is the same
// as the console, but without the timestamp (which is expected to be
// set by syslogd).

235
void LoggerManagerImpl::setSyslogAppenderLayout(
236
237
238
        log4cplus::SharedAppenderPtr& appender)
{
    // Create the pattern we want for the output - local time.
239
    string pattern = "%-5p [%c] %m\n";
240
241
242
243
244
245

    // Finally the text of the message
    auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(pattern));
    appender->setLayout(layout);
}

Stephen Morris's avatar
Stephen Morris committed
246
247
} // namespace log
} // namespace isc