rm5248 commented on PR #159:
URL: https://github.com/apache/logging-log4cxx/pull/159#issuecomment-1350393300
To expand on my previous comment, I would propose that the user guide look
something like the following, basically showing the two options on how to use.
The first example is pretty much what exists today, but more simplified. The
second example is what you have here.
For the first-time user the idea with the first example is that because this
is only 30 lines, it is very easy to copy and paste into a scratch project to
test things out with.
-----
# Configuration
## A Simple Example
In order to start using Log4cxx, a simple example program is shown below.
This program does nothing useful, but it shows the basics of how to start using
Log4cxx. Using the BasicConfigurator class, we are able to quickly configure
the library to send data to standard error at the INFO level.
```
// include log4cxx header files.
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
using namespace log4cxx;
static LoggerPtr logger(Logger::getLogger("MyApp"));
void foo(){
// Get a logger that is a child of the statically declared logger
LoggerPtr fooLogger(Logger::getLogger("MyApp.foo");
LOG4CXX_TRACE(fooLogger, "Doing foo at trace level");
LOG4CXX_DEBUG(fooLogger, "Doing foo at debug level");
LOG4CXX_INFO(fooLogger, "Doing foo at info level");
LOG4CXX_WARN(fooLogger, "Doing foo at warn level");
LOG4CXX_ERROR(fooLogger, "Doing foo at error level");
LOG4CXX_FATAL(fooLogger, "Doing foo at fatal level");
}
int main(int argc, char **argv)
{
int result = EXIT_SUCCESS;
// Set up a simple configuration that logs on the console.
BasicConfigurator::configure();
LOG4CXX_INFO(logger, "Entering application.");
foo();
LOG4CXX_INFO(logger, "Exiting application.");
return result;
}
```
The above application does nothing useful except to show how to initialize
logging with the BasicConfigurator and do logging with different loggers. Note
that file based configurations are also possible - see DOMConfigurator and
PropertiesConfigurator.
This example also has some limitations in how logging is initialized:
- Any logging in static initializers will be lost, as the logging is not
initialized before main() starts
- (some other limitation??)
## Configuration for use with static initialization
(your example here)
There are some advantages to this approach:
- You are able to log in static initializers when configuring through code
if the default initialization procedure is not appropriate
- (some other advantage that I don't know about??)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]