Apache Log4cxx
Version 2.0.0
Toggle main menu visibility
Loading...
Searching...
No Matches
custom-appender.cpp
This example shows how to extend Log4cxx with a new appender.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <
log4cxx/basicconfigurator.h
>
#include <
log4cxx/appenderskeleton.h
>
#include <
log4cxx/helpers/stringhelper.h
>
#include <
log4cxx/xml/domconfigurator.h
>
namespace
com::foo {
class
NullWriterAppender :
public
log4cxx::AppenderSkeleton
{
public
:
DECLARE_LOG4CXX_OBJECT
(NullWriterAppender)
BEGIN_LOG4CXX_CAST_MAP
()
LOG4CXX_CAST_ENTRY
(NullWriterAppender)
LOG4CXX_CAST_ENTRY_CHAIN
(AppenderSkeleton)
END_LOG4CXX_CAST_MAP
()
NullWriterAppender(){}
void
append(
const
log4cxx::spi::LoggingEventPtr
& )
override
{
// This gets called whenever there is a valid event for our appender.
}
void
activateOptions()
override
{
// Given all of our options, do something useful(e.g. open a file)
}
void
setOption(
const
log4cxx::LogString
& option,
const
log4cxx::LogString
& value)
override
{
if
(
log4cxx::helpers::StringHelper::equalsIgnoreCase
( option
, LOG4CXX_STR(
"SOMEVALUE"
)
, LOG4CXX_STR(
"somevalue"
)
))
{
// Do something with the 'value' here.
(void)value;
}
}
};
// Register our appender with Log4cxx at startup
IMPLEMENT_LOG4CXX_OBJECT
(NullWriterAppender)
}
// namespace com::foo
int
main()
{
log4cxx::xml::DOMConfigurator::configure
(
"custom-appender.xml"
);
auto
rootLogger =
log4cxx::Logger::getRootLogger
();
auto
nullLogger =
log4cxx::Logger::getLogger
(
"NullLogger"
);
LOG4CXX_INFO
( rootLogger,
"This is some root message"
);
LOG4CXX_INFO
( nullLogger,
"This message will be discarded"
);
}
appenderskeleton.h
basicconfigurator.h
log4cxx::AppenderSkeleton
Implementation base class for all appenders.
Definition
appenderskeleton.h:39
log4cxx::Logger::getLogger
static LoggerPtr getLogger(const std::string &name)
Retrieve a logger by name in current encoding.
log4cxx::Logger::getRootLogger
static LoggerPtr getRootLogger()
Retrieve the root logger.
log4cxx::helpers::StringHelper::equalsIgnoreCase
static bool equalsIgnoreCase(const LogString &s, const logchar *upper, const logchar *lower)
Is each character in s identical to the character at the corresponding position in either upper or lo...
log4cxx::xml::DOMConfigurator::configure
static spi::ConfigurationStatus configure(const File &configFilename)
Apply the directives in configFileName to the spi::LoggerRepository held by LogManager.
domconfigurator.h
LOG4CXX_INFO
#define LOG4CXX_INFO(logger, message)
Add a new logging event containing message to attached appender(s) if logger is enabled for INFO even...
Definition
log4cxx/logger.h:1422
log4cxx::spi::LoggingEventPtr
std::shared_ptr< LoggingEvent > LoggingEventPtr
Definition
appender.h:32
log4cxx::LogString
std::basic_string< logchar > LogString
Definition
logstring.h:51
LOG4CXX_CAST_ENTRY
#define LOG4CXX_CAST_ENTRY(Interface)
Definition
object.h:153
IMPLEMENT_LOG4CXX_OBJECT
#define IMPLEMENT_LOG4CXX_OBJECT(object)
Definition
object.h:62
END_LOG4CXX_CAST_MAP
#define END_LOG4CXX_CAST_MAP()
Definition
object.h:147
DECLARE_LOG4CXX_OBJECT
#define DECLARE_LOG4CXX_OBJECT(object)
Definition
object.h:41
LOG4CXX_CAST_ENTRY_CHAIN
#define LOG4CXX_CAST_ENTRY_CHAIN(Interface)
Definition
object.h:159
BEGIN_LOG4CXX_CAST_MAP
#define BEGIN_LOG4CXX_CAST_MAP()
Definition
object.h:141
stringhelper.h
Generated by
1.18.0 on
SITE_PUBLISHED_DATE
Copyright © 2017-2026
Apache Software Foundation
. Apache, Chainsaw, log4cxx, Log4j, Log4net, log4php and the Apache logo are
trademarks or registered trademarks
of The Apache Software Foundation.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Privacy Policy
.