logger_manager_impl.cc 7.86 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
20
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/consoleappender.h>
Stephen Morris's avatar
Stephen Morris committed
21
#include <log4cplus/fileappender.h>
22
#include <log4cplus/syslogappender.h>
Stephen Morris's avatar
Stephen Morris committed
23

24
25
26
27
28
29
30
#include <log/logger.h>
#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>
31
32
33

using namespace std;

Stephen Morris's avatar
Stephen Morris committed
34
35
36
namespace isc {
namespace log {

Stephen Morris's avatar
Stephen Morris committed
37
38
39
40
41
// 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
42
43

void
44
LoggerManagerImpl::processInit() {
Stephen Morris's avatar
Stephen Morris committed
45
    log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
46
    initRootLogger();
Stephen Morris's avatar
Stephen Morris committed
47
48
49
50
51
52
53
54
}

// Process logging specification.  Set up the common states then dispatch to
// add output specifications.

void
LoggerManagerImpl::processSpecification(const LoggerSpecification& spec) {

55
56
    log4cplus::Logger logger = log4cplus::Logger::getInstance(
                                   expandLoggerName(spec.getName()));
Stephen Morris's avatar
Stephen Morris committed
57
58
59
60
61
62
63
64

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

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

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    // Output options given?
    if (spec.optionCount() > 0) {

        // Yes, so replace all appenders for this logger.
        logger.removeAllAppenders();

        // 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:
84
                createSyslogAppender(logger, *i);
85
86
87
                break;

            default:
88
89
90
91
                // 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.
92
93
94
95
                isc_throw(UnknownLoggingDestination,
                          "Unknown logging destination, code = " <<
                          i->destination);
            }
Stephen Morris's avatar
Stephen Morris committed
96
97
98
99
100
101
102
103
104
105
106
107
        }
    }
}

// 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));
108
    setConsoleAppenderLayout(console);
Stephen Morris's avatar
Stephen Morris committed
109
110
111
    logger.addAppender(console);
}

Stephen Morris's avatar
Stephen Morris committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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)
{
    LOG4CPLUS_OPEN_MODE_TYPE mode = 
        LOG4CPLUS_FSTREAM_NAMESPACE::ios::app;  // Append to existing file

    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);
}

136
// Syslog appender. 
137
void
138
LoggerManagerImpl::createSyslogAppender(log4cplus::Logger& logger,
139
140
141
142
                                         const OutputOption& opt)
{
    log4cplus::SharedAppenderPtr syslogapp(
        new log4cplus::SysLogAppender(opt.facility));
143
    setSyslogAppenderLayout(syslogapp);
144
145
146
    logger.addAppender(syslogapp);
}

Stephen Morris's avatar
Stephen Morris committed
147

148
149
150
// One-time initialization of the log4cplus system

void
151
152
LoggerManagerImpl::init(isc::log::Severity severity, int dbglevel) {

153
154
155
156
157
158
159
160
161
162
    // 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();

163
    reset(severity, dbglevel);
Stephen Morris's avatar
Stephen Morris committed
164
165
166
167
168
169
}

// 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
170
LoggerManagerImpl::reset(isc::log::Severity severity, int dbglevel) {
Stephen Morris's avatar
Stephen Morris committed
171
172

    // Initialize the root logger
173
    initRootLogger(severity, dbglevel);
174
175
176
}

// Initialize the root logger
177
void LoggerManagerImpl::initRootLogger(isc::log::Severity severity,
Stephen Morris's avatar
Stephen Morris committed
178
179
180
                                       int dbglevel)
{
    log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
181

182
183
184
    // Set the log4cplus root to not output anything - effectively we are
    // ignoring it.
    log4cplus::Logger::getRoot().setLogLevel(log4cplus::OFF_LOG_LEVEL);
185

186
187
188
189
190
191
192
193
    // 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)));

    // Set the BIND 10 root to use a console logger.
Stephen Morris's avatar
Stephen Morris committed
194
    OutputOption opt;
195
    createConsoleAppender(b10root, opt);
196
197
198
199
200
201
}

// Set the the "console" layout for the given appenders.  This layout includes
// a date/time and the name of the logger.

void LoggerManagerImpl::setConsoleAppenderLayout(
202
        log4cplus::SharedAppenderPtr& appender)
203
204
{
    // Create the pattern we want for the output - local time.
205
    string pattern = "%D{%Y-%m-%d %H:%M:%S.%q} %-5p [%c] %m\n";
206
207
208
209
210
211

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

212
213
214
215
// 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).

216
void LoggerManagerImpl::setSyslogAppenderLayout(
217
218
219
        log4cplus::SharedAppenderPtr& appender)
{
    // Create the pattern we want for the output - local time.
220
    string pattern = "%-5p [%c] %m\n";
221
222
223
224
225
226

    // 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
227
228
} // namespace log
} // namespace isc