LOGGER IN JAVA

Logger in Java

    ------------------


Logging is the act of writing information about events in the application to a file, also called the log file.


Why do we need logging?
----------------------------------------

1) Troubleshooting

2) Performance Analytics

3) Auditing - safety and traffic


Who will benefit?
--------------------

1) Software Developers

2) System Administrators

3) Support Team


When to use Logging?
---------------------------

1) Exceptions and errors

2) Events and States

3) Debug info

4) HTTP requests - if our app uses an external API we might send the log request to it and the response we are getting.

5) Thread Usage - deadlock problem analysis

6) Frontend errors


Different Loggers Available/Logging Framework:-
---------------------------------------------------------

1) Very popular library: log4j

2) Since java 1.4: java.util.Logging

3) LogBack

4) Commons Logging


Log Levels:-
---------------




Log levels represent the severity of the situation. There are 7 logging levels:-

1) SEVERE

2) WARNING

3) INFO

4) CONFIG

5) FINE

6) FINER

7) FINEST


Log Handlers:-
--------------------

1) Sends the message to the log place

2) Handlers format the message using a formatter.

3) Handlers intercept to log rockets and it processes them before they get set onto the log.


Types of Handlers:-
-----------------------

1) ConsoleHandler - default Handler

2) FileHandler

3) StreamHandler

4) SocketHandler

5) MemoryHandler


Logging Best Practices:-
----------------------------

1) Be precise

2) No Sensitive data

3) Right log level

4) Machine and human-readable

5) Don't log too much

6) Don't log too little


Different Log Methods:-
-----------------------------

1) LOGGER.log(Level.INFO, "This is an example message");

2) LOGGER.logp(Level.INFO, LogExample.class.getName(), "sourceMethodName", "This is an example message");

3) LOGGER.logrb(Level.INFO, ResourceBundle.getBundle("en_US"), "This is an example message");


Logging Filter:-
---------------------

Filter is an interface. The isLoggable method must be implemented. If it returns true for a certain log message, it will be sent to the handler and will be logged. If it returns false, it will be ignored.

Filter can be set on the logger.


LogManager:-
-----------------

1) The LogManager is the class responsible for managing the logging hierarchy.

2) It loads the configuration for all the loggers. This can be done through a special config class or a config file.

3) LogManager is a Singleton.


The Best Spring Data JPA Logging Configuration in Spring Boot:-
------------------------------------------------------------------------------

Logging Configuration for Development:-
------------------------------------------------

# Generate and log statistics

spring.jpa.properties.hibernate.generate_statistics=true

logging.level.org.hibernate.stat=DEBUG


# Log slow queries

spring.jpa.properties.hibernate.session.events.log.LOG_QUERIES_SLOWER_THAN_MS=1


# Log all SQL statements

logging.level.org.hibernate.SQL=DEBUG


# Logging cache operations

logging.level.org.hibernate.cache=DEBUG


Logging Configuration for Production:-
-----------------------------------------------

spring.jpa.properties.hibernate.generate_statistics=false

logging.level.org.hibernate=ERROR


0 Comments