Skip to content

Commit 29ea9fa

Browse files
add recommended library
1 parent 7ab4f93 commit 29ea9fa

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

docs/java/logging.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,36 @@ Logging runtime information in your Java application is critically useful for un
88

99
In a real-world production environment, you usually don’t have the luxury of debugging. And so, logging files can be the only thing you have to go off of when attempting to diagnose an issue that’s not easy to reproduce.
1010

11-
### Logging Conventions:
11+
### Logging Conventions
12+
1213
* For consistency, declare your logger as the first field (top of code) in your class and declare it as follows:
13-
- private static final Logger logger = Logger.getLogger(MyClassName.class.getName());
14+
- private static final Logger logger = Logger.getLogger(MyClassName.class.getName());
1415
The variable name should be "logger". The term "logger" makes for cleaner code while not reducing any developer's ability to understand the code.
1516

1617
* Never log private or sensitive information such as **user credentials** or **financial data**. The data which should remain private must not be logged.
17-
* Never log too much information. This can happen in an attempt to capture all potentially relevant data. Too many log messages can also lead to difficulty in reading a log file and identifying the relevant information when a problem does occur. Always use cross-cutting concerns, an _Aspect_, to log the entry and exit of a method.
18-
* Sufficient information must be provided in the logged message.
18+
* Never log too much information. This can happen in an attempt to capture all potentially relevant data. Too many log messages can also lead to difficulty in reading a log file and identifying the relevant information when a problem does occur. Always use cross-cutting concerns, an _Aspect_, to log the entry and exit of a method.
19+
* Sufficient information must be provided in the logged message.
1920
An example of a log message lacking specificity:
2021
- Error! Operation Failed
2122
Always add more specific and identifiable message:
2223
- Error! File upload profile_picture.jpg failed for unitId: 2
2324
* Add context in the log message by including the **timestamp**, **log level**, **thread name**, **fully qualified class name** and the **method name** of the event. This information can be hardwired in the configuration for the logging-framework used.
2425
* Use appropriate **LOG Levels**
25-
- **FATAL**
26+
* **FATAL**
2627
FATAL should be reserved for errors that cause the application to crash or fail to start (ex: JVM out of memory)
27-
- **ERROR**
28+
* **ERROR**
2829
ERROR should contain technical issues that need to be resolved for proper functioning of the system (ex: couldn’t connect to database)
29-
- **WARN**
30+
* **WARN**
3031
WARN is best used for temporary problems or unexpected behavior that does not significantly hamper the functioning of the application (ex: failed user login)
31-
- **INFO**
32+
* **INFO**
3233
Use INFO to report results to Administrators and Users. INFO should contain messages that describe what is happening in the application (ex: user registered, order placed)
33-
- **DEBUG**
34-
Use DEBUG to report results to yourself and other developers. DEBUG is intended for messages that could be useful in debugging an issue
35-
- **TRACE**
36-
Use TRACE only for tracing the execution flow. In general, trace-level logging should be used only for tracing the flow of execution through a program and for flagging particular positions in a program.
34+
* **DEBUG**
35+
Use DEBUG to report results to yourself and other developers. DEBUG is intended for messages that could be useful in debugging an issue
36+
**TRACE**
37+
Use TRACE only for tracing the execution flow. In general, trace-level logging should be used only for tracing the flow of execution through a program and for flagging particular positions in a program.
38+
39+
### Logging Libraries
40+
41+
* <a href="https://logging.apache.org/log4j/2.x/" target="_blank">Apache Log4j2 (recommended)</a>
42+
43+
* <a href="http://logback.qos.ch/manual/configuration.html" target="_blank">Logback</a>

0 commit comments

Comments
 (0)