[Java log] P1: The importance of logging in software development

Articles published from blog Tung Huynh, was the consent of the author.

Hi everybody. These days, busy too, enlist new forever be sitting writing. Today I will share their experiences related to the issue of logging in software development.

What is log?

Comes to logging, it must have many of you know this is a jobtracking the processof 1 code / function / application, or record the error occurred (if there)
All the major systems such as system administrator database logging all user details publicexecute query what's onsessionWhat time does. there are Microsoft, web browser, adobe,... contains the log function to record and display windowsSend reportfor software developers when an error occurs.

Kaº¿t quẠ£ £ nh hình Ạto send the report window

Logging to do?

So keep track finished, to do? Of course, serving the tracing already.
If you log error, when the program errors you can see what error, in any code and easy to find problem solution. Many cases of program failures in the production environment that unable to reproduce again, That is when the need to log to find the cause.
If you record user logs application, you will know the user does on access the application and manipulation what, from which to find the perpetrators of the vandalism cases.
If logging in complex code, the background process, you will know status của tiến trình đó đang chạy như thế nào, biết được tốc độ xử lý của các nghiệp vụ phức tạp, có cơ sở để tìm ra các đoạn code gây cao tải cho hệ thống.
Và rất nhiều lợi ích khác 🙂

There are certain ways of logging?

  • Print ra màn hình
  • Ghi ra file lưu trên ổ cứng
  • Kết nối ftp đến server khác để lưu file và truyền file sang server lưu log, hoặc gửi qua service, email đến nhà phát triển.
  • Insert log vào 1 bảng trong cơ sở dữ liệu

Logging by what?

  • Print screen command
  • Using log4j library, slf4j in java
  • Insert log into the database by the execute SQL or ORM like Hibernate,...
  • Build your own logging module

Logging of something?

Not scored more than That's good, record too will do redundant log difficult to control and trace logs, storage costs to be saved, or wash away the logs needed
Not possible scored too few leading to no logging to handle
To enough log records then we offer a number of positions as follows:

  • In the blocks catch exception, need to log to find out position defect in applications when problems occur.
  • These functional use, ability causing high loads system, the important functions required for logging regular monitoring, ensure that the system is still running stable.
  • With the background process, minimum need to log every time start and finish together with the total Processing Time to know the process is running on schedule laid or not, processing speed faster or slower. With large long-running process has many steps should log whenever started or completed 1 Steps to monitoring processes are handled in step, errors or rollback in step. If necessary, it can record information such as current server status Ram, CPU, Disk IO,... to base risk assessment of high system load, scheduled plans to restrict much progress processes and run concurrently.
  • In the complex business many processing steps should also log step by step similar process. Or code Potential risks eg not too important functions are handled by temporary yet to find the optimal plan, or time release the fold should log monitoring logic abnormalities or not to handle instantaneous.
  • log when communicate with other systems. In here we have 2 way to communicate that is the api / public service for other systems connect to, or fragments referred to api / service of other systems, or any other connection to deploy by writing code. All need logging Half basic information as: Link Fire, path / processing methods, Newsletter request, newsletters response, Processing Time. And it is better to log into £ in database when needed most convenient to trace log. Because of the integration between the systems often happens a lot of problems, if there is no specific log will be difficult to identify any error by the, lead to Blaming responsibility for each other and do not solve the problem.
  • Also some system or important function, need specific log impact change data in database. Insert / update / delete / select to record any, data before and after change. To control the impact on the data and rollback data when necessary.
  • Or case of problems on the server, not reappear on local, nor debug on the server, only the additional logging to the suspected location to find fault

Logging how?

When logging need to ensure some rule as follows:
- If the error log, the application still has to run normally. Although on his part refers to many issues expressed the importance of logging, but the main goal is still only benefits the operation of the system, also for end-user then What no meaning. So if error logging errors that do both primary services should best not to logging.
- Log processing time not affect the processing time professional. Whether logging out or insert the file into a database or stored anywhere, it will take 1 of time to handle, but despite only takes a few minutes or a few milliseconds, it also should not let it affect the total processing time of business. Because when encountering large trading business continuity is a price to pay for time logging multiplied not small
From his principles on a number advice respectively when logging:
- Always catch all exceptions of the log when the code embedded in the business of software functions, ensure there are no errors that might throw from handling logging out primary service.
- Logging asynchronously equal 1 other processes, The simplest is to run it in 1 own thread.
+ Avoid using command System.out.print (or similar) to print the log out screen for screen printing is a sequential operation performed very slowly
+ avoid e.printStackTrace() to print the log out screen, also due on
+ Candlestick use the logging library asynchronously as Log4j in java. Looking through log4j still just 1 command logger.infor("Nội dung")but actually inside that is both 1 processing system. Content should log will be submit on 1 queue, Besides, there will be 1 background process scanning queue to retrieve the contents of the logging, Such ensures logging does not affect the processing time professional.

Monitoring Log?

Now that the software has, the next log is used to log data that the task of monitoring system. There are various ways through log monitoring system as
- Open the log file by hand, typing manually select the log table, and log information read by eye This requires the supervisor must have knowledge of software and databases as well as access to most of the resources above to view log information. This is quite common in the development environment for instant readability log, Compact fast.
- Also on the environment production, supervisors may not have knowledge of software, they just sit and let live system technical team when the problem. So this time the software needs more monitoring functions an intuitive system with picture, color, data information to be displayed as a table with rows and columns explicitly. To do this, there are many treatment
+ integrated systems monitoring logs available as JMX Remote, Java Profiler, Nagios, Spring Boot Actuator or as a complete set Logstash (realtime data collection log) + ElasticSearch/Solr (data analysis log, full text search quickly into optimum) + Kibana (interface displays information log data 1 visually from ElasticSearch)

Kaº¿t quẠẠhình £ £ nh for kibana elasticsearch logstash

Self builda tool (simple or complex depending on demand) to read data log file, analyze and display a warning on the interface this way is not recommended because many systems are available above did well, high compatibility that is free

Through this article you have grasped the basicsimportance of logging in software development, and someruleneed to be aware when logging. In future articles I will present specific examples to integratemodule loggingBasic standards most insoftware.