# 04.3-Security and DevOps Lesson3:Logging
###### tags: `Udacity`
# 01 Logging
{%youtube Zek2MbmVtKs%}
**Lesson Outline**
The prime objective of this lesson is to describe SLF4J logging API and Log4J logging framework for code insight and debugging. To achieve that, the following are the sub-objectives of this lesson:
- Explain the basics of Logging
- Introduce the SLF4J library (API) and Log4J framework
- Demonstrate the implementation of Log4J in the eCommerce application
# 02 SLF4J and Log4J
**SLF4J and Log4j 簡易日誌門面**
Simple Logging Facade for Java (SLF4J) is an API that can be used as an abstraction layer for several logging frameworks. Examples include java.util.logging, logback and Log4j. It is one of the best practices to use SLF4J in enterprise application development. SLF4J in combination with any logging framework provides a complete logging solution. We have chosen Log4j as our preferred logging framework.
{%youtube SyLVZsE3_UQ%}
**SLF4J**
{%youtube ksA0EZpwSZU%}
SLF4J allows the end-user to plug-in the desired logging framework at deployment time. Using SLF4J is particularly useful for enterprise applications because individual embedded components have different logging requirements. In addition, SLF4J API is backward compatible and easy to implement. For stand-alone applications, we may invoke the logging framework of our choice directly.
**Download Log4J framework**
Log4j framework can be downloaded from [here](http://logging.apache.org/log4j/2.x/download.html).
Remember that whenever we download a framework in Java, we would need to include the dependencies (APIs and .jars) in our application’s classpath. For Log4j v2, we would need to add the following dependencies:
```typescript
log4j-api-2.13.0.jar
log4j-core-2.13.0.jar
```
Also, we would need to include the Log4j Maven dependency into `pom.xml` as:
```xml
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
```
Similarly, a suitable SLF4J-Log4j Maven dependency can be found [here](https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12). Note the version in the above dependencies. Version may change over time. Upcoming videos would demonstrate the usage of Log4J v1. We recommend you to use Log4j v2, as it provides many of the improvements available in Logback. The choice of Log4j v1.x versus Log4j v2.x does not afford much advantage either way.
You can check out the SLF4J manual [here](https://www.slf4j.org/manual.html) and Log4j manual [here](https://logging.apache.org/log4j/2.0/manual/index.html). If you don't want to read it now, then we suggest you still have a quick look and bookmark it for later reference!
# 03 Log Levels
**Log Levels**
{%youtube Ji9CeW7trDE%}
Log4J is most often used to create an instance of the Logger interface from the LogManager and then call the methods on this interface. Assume there is a `<ClassToTest>.java`, inside which we need to create an instance of `Logger` interface by using either of the following ways.
```java
public static final Logger log = LogManager.getLogger(<ClassToTest>.class)
```
or
```java
public static final Logger log = LoggerFactory.getLogger(<ClassToTest>.class)
```
. Then, we can use any of the in-built log-levels in any method of `<ClassToTest>.java`, as follows:
```java
// ...
logger.trace("the built-in TRACE level");
logger.debug("the built-in DEBUG level");
logger.notice("a custom level: a NOTICE message");
logger.info("the built-in INFO level");
logger.warn("the built-in WARN level");
logger.error("the built-in ERROR level");
logger.fatal("the built-in FATAL level");
// ...
```
Following are the in-built log-levels in highest to lowest order of logging:
- Log-Level and Description
1. Trace: Logs the fine-grained information. This is a high (most detailed) level of logging.
2. Debug: Logs the information necessary for debugging.
3. Info: Used for logging the status messages or any desirable field value.
4. Warn: Used for logging potentially unexpected/dangerous situations
5. Error: Used for logging Exception and minor Error events
6. (最高級別)Fatal: Used for logging very severe Error events that might lead the application to collapse.
Log-Level:
`FATAL > ERROR > WARN > INFO > DEBUG`
- Debug
- The lowest level, for logging incredibly detailed statistics and other detailed information
- Info
- For logging standard, non-error information
- Warn
- For logging non-fatal errors that can maybe be recovered from
- Error
- For logging fatal errors that are unrecoverable
# 04 Log4J Example
{%youtube OVZuylA3zAE%}
**Conclusion**
We have learnt the following topics, as promised at the beginning of this lesson:
- Basics of Logging
- Fundamentals of the SLF4J library (API) and Log4J framework
- Implementation of Log4J in the eCommerce application