เรื่อง 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

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

https://www.crowdstrike.com/cybersecurity-101/observability/debug-logging/

DEBUG

console.log('==========');
console.log(err);
DEBUG: Fetching mailing list {"listid":14777}

or TRACE: Fetching mailing list {"listid":14777}

or Unit Testing

INFO

level=INFO msg=Connecting to the server...
level=INFO msg=No Content.

WARN

level=WARN msg=WARNING WARNING WARNING
level=WARN msg=context deadline exceeded

or level=ERROR msg=context deadline exceeded

or add to metric

ERROR

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

ERROR: but the response itself says 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

Common Mistakes

slog.Error("not found")
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

Sesitive 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

Cloud Cost

https://cloud.google.com/blog/products/devops-sre/cloud-logging-cost-management-best-practices

  1. Analyze your current spending on logging tools
  2. Eliminate waste — don't pay for logs you don't need
  3. Optimize costs over the lifecycle of your logs
  4. Setup alerts to avoid surprise bills

Who needs it?