You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/java/logging.md
+19-12Lines changed: 19 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,29 +8,36 @@ Logging runtime information in your Java application is critically useful for un
8
8
9
9
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.
10
10
11
-
### Logging Conventions:
11
+
### Logging Conventions
12
+
12
13
* 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());
14
15
The variable name should be "logger". The term "logger" makes for cleaner code while not reducing any developer's ability to understand the code.
15
16
16
17
* 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.
19
20
An example of a log message lacking specificity:
20
21
- Error! Operation Failed
21
22
Always add more specific and identifiable message:
22
23
- Error! File upload profile_picture.jpg failed for unitId: 2
23
24
* 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.
24
25
* Use appropriate **LOG Levels**
25
-
-**FATAL**
26
+
***FATAL**
26
27
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**
28
29
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**
30
31
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**
32
33
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.
0 commit comments