เรื่อง Log เรื่องเล็ก เรื่องไม่ Log เรื่องใหญ่
Pallat Anchaleechamaikorn
Technical Coach
Infinitas by KrungThai
Arise by Infinitas
The Ship’s Log
Is logging a requirement?
Let’s focus on debug logging
Debug Logging Level
%%{
init: {
'theme': 'base',
'themeVariables': {
'fontSize': '40px',
'primaryColor': '#CE3262',
'primaryTextColor': '#fff',
'primaryBorderColor': '#7C0000',
'lineColor': '#F8B229',
'secondaryColor': '#006100',
'tertiaryColor': '#fff'
}
}
}%%
graph RL
Trace --> Debug
Debug --> Info
Info --> Warn
Warn --> Error
Error --> Fatal
style Error fill:#C8647E
style Warn fill:#D08096
style Info fill:#D99BAD
style Debug fill:#E2B4C0
style Trace fill:#EBCAD4
FATAL ERROR WARN INFO DEBUG TRACE OFF FATAL x ERROR x x WARN x x x INFO x x x x DEBUG x x x x x TRACE x x x x x x
Choose the Right Log Level
DEBUG
Used in the development process INFO
regular operation WARN
system instability or connectivity issues ERROR
runtime issues FATAL
generally result in the shutdown https://www.crowdstrike.com/cybersecurity-101/observability/debug-logging/
DEBUG: ใช้เฉพาะใน Dev En เพื่อวิเคราะห์แอป
INFO: ใช้แสดงว่าแอปทำงานได้ปกติหรือไม่ เช่น startup/shutdown
WARN: ปัญหา runtime ที่อาจจะไม่ส่งผลกระทบโดยตรง แต่อาจจะมีผลกระทบอื่นๆตามมา ส่วนมากเกี่ยวกับความเสภียร
ERROR:
FATAL: รุนแรงจนต้องระงับการทำงาน
DEBUG
%%{
init: {
'theme': 'base',
'themeVariables': {
'primaryColor': '#D99BAD',
'primaryTextColor': '#fff',
'primaryBorderColor': '#7C0000',
'lineColor': '#F8B229',
'secondaryColor': '#006100',
'tertiaryColor': '#fff'
}
}
}%%
graph RL
Trace --> Debug
Debug --> Info
Info --> Warn
Warn --> Error
Error --> Fatal
style Debug fill:#CE3262
console .log ('==========' );
DEBUG : Fetching mailing list {"listid" : 14777 }
or
TRACE : Fetching mailing list {"listid" : 14777 }
or
Unit Testing
INFO
%%{
init: {
'theme': 'base',
'themeVariables': {
'primaryColor': '#D99BAD',
'primaryTextColor': '#fff',
'primaryBorderColor': '#7C0000',
'lineColor': '#F8B229',
'secondaryColor': '#006100',
'tertiaryColor': '#fff'
}
}
}%%
graph RL
Trace --> Debug
Debug --> Info
Info --> Warn
Warn --> Error
Error --> Fatal
style Info fill:#CE3262
level= INFO msg= Connecting to the server...
level= INFO msg= No Content.
WARN
%%{
init: {
'theme': 'base',
'themeVariables': {
'primaryColor': '#D99BAD',
'primaryTextColor': '#fff',
'primaryBorderColor': '#7C0000',
'lineColor': '#F8B229',
'secondaryColor': '#006100',
'tertiaryColor': '#fff'
}
}
}%%
graph RL
Trace --> Debug
Debug --> Info
Info --> Warn
Warn --> Error
Error --> Fatal
style Warn fill:#CE3262
level= WARN msg= WARNING WARNING WARNING
level= WARN msg= context deadline exceeded
or
level= ERROR msg= context deadline exceeded
or
add to metric
ERROR
%%{
init: {
'theme': 'base',
'themeVariables': {
'primaryColor': '#D99BAD',
'primaryTextColor': '#fff',
'primaryBorderColor': '#7C0000',
'lineColor': '#F8B229',
'secondaryColor': '#006100',
'tertiaryColor': '#fff'
}
}
}%%
graph RL
Trace --> Debug
Debug --> Info
Info --> Warn
Warn --> Error
Error --> Fatal
style Error fill:#CE3262
level= ERROR msg= Get "http://example.com/api" : dial tcp: lookup example.co: no such host
How about if it’s ok but http status is 404
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns= "http://www.w3.org/1999/xhtml" xml:lang= "en" lang= "en" >
<head>
<title> 404 - Not Found</title>
</head>
<body>
<h1> 404 - Not Found</h1>
<script type= "text/javascript" src= "//obj.ac.bcon.ecdns.net/ec_tpm_bcon.js" ></script>
</body>
</html>
Common Mistakes
Ignoring important logs (Error without log) Use Fatal in libraries Logging at the wrong level Useless message Sensitive Information Common Mistakes
time=2023-11-14T21:41:20.632+07:00 level=ERROR source=handler.go:25 msg=not found
slog .Error ("result failed" )
time=2023-11-14T21:41:20.632+07:00 level=ERROR source=handler.go:25 msg=result failed
10 Rules to Microservice Logging
https://medium.com/@sniederm/10-rules-to-microservice-logging-4d24f6dd7abb
Rule 1: Write your logs to console, not files Rule 2: Use a logging framework Rule 3: Log in JSON format where possible Rule 4: Centralize and index your logs Rule 5: Treat logs like event streams Rule 6: Do not log sensitive and privacy data Rule 7: Use common log levels (DEBUG, INFO, WARN, ERROR) Rule 8: Use a correlation / trace ID Rule 9: Use a reporting tool Rule 10: Keep an eye on complexity and performance Sesitive Data
Personally Identifiable Information (PII) Financial Information Health and Medical Information Authentication Credentials Confidential Business Data Legal and Compliance Data Personal Communications Biometric Data <https://betterstack.com/community/guides/logging/sensitive-data/>
Logging too much data can be distracting and a poor use of resources. Indeed, transferring, storing and parsing logs is expensive, so minimizing what the log files contains can minimize cost and resources. Still, logging is king, especially when it comes to traditional monolithic architectures
https://www.bmc.com/blogs/monitoring-logging-tracing/
Who cares about this kind of message?
level= INFO msg= PaymentService - Payment processed successfully for Order #12345
What’s the log level in Production?
ERROR
WARN
INFO
DEBUG