Java Logging
Please share your thoughts about whether to keep this book at Wikibooks. Maintain this notice so this book can receive a fair review and people have time to boldly address reasonable concerns through concrete improvements. Remove or replace this notice after discussion concludes and a community decision is made. Please also consider notifying the primary contributors on their discussion page with
|
This book is an undeveloped draft or outline. You can help to develop the work, or you can ask for assistance in the project room. |
(Fancy TimePlot image here)
Usages for logs:
- Allowing programs to indicate emergency situations to external programs which can then investigate and take action.
- Provide timestamped markers for performance analysis.
- Provide information about what is being processed for later forensic analysis.
- Timestamps for events.
Logging in programming is one of the areas where the code may literally have been written years before the generated information is needed, and where you have very little or no idea what you might be needing to know at that time. If you could foresee it, you could have coded around it back then.
There is a need for a wiki-based reference helping programmers to create better logs. The focus will not be a comprehensive overview, but a "Best practice"-approach using slf4j and logback/java.util.logging.Logger depending on scenario.
Contents
[edit | edit source]Quick start for the impatient
[edit | edit source]- Download and unpack the latest release of SLF4J from http://slf4j.org/download.html
- Put slf4j-api-X.Y.Z.jar and slf4j-simple-X.Y.Z.jar on your classpath.
- Use
Logger log = LoggerFactory.getLogger(this.getClass())
andlog.info("At {} with {}", a, b);
to log information.
((SAMPLE CODE))
Logging: Why, How, What, Where?
[edit | edit source]- SAMPLE Java program with print statements.
- Output from said java program.
One of the first things new Java programmers learn is to use System.out.println("....")
to see "inside" their program, and frequently this results in a lot of information being printed every time the program runs. Some information is very important and require human action, and some are just trivial indications that a library is working, and some include a stack trace. For advanced programs with multiple threads active, the individual messages may even be mixed together so that "Message 1" and "Message 2" turn into "MMessagessag 1e 2". Also you may need the log in a file and not System.out (which for some platforms are not even available).
- SAMPLE Java program as before but with slf4j statements.
- Resulting logfiles.
Generating log entries
[edit | edit source]- What is SLF4J and why should it be used?
- Examples of typical log statements
- Using ERROR, WARN, INFO, DEBUG, TRACE levels
- Improving stack traces
- Improve existing log statements
- Add logging to existing jars
Saving log entries for later
[edit | edit source]- Choosing a framework
- Choosing events to save
- Choosing where to persist events
- Choosing how long to save events
- Taking immediate action on log entries