Coding Gun

วิธีการดู logs ใน container

เราสามารถเข้าไปดู Log file ของ container ได้ด้วยคำสั่ง

docker container logs [option] [Container ID]

หรือเขียนสั้นๆว่า

$ docker logs [option] [Container ID]

โดยที่ options ต่างๆ ที่เราจะใช้บ่อยๆ มีดังนี้

Save log จาก console ไปยัง file

ถ้าเราจะ save logs ที่เราต้องการออกไปเป็น file เราจะใช้คำสั่ง

$ docker logs --since=1h [Container ID] &> nginx.log

Path ที่เก็บ Logfile

เราสามารถดึง path ที่เก็บ log file ได้ด้วยคำสั่ง

$ docker inspect --format='{{.LogPath}}' [containerID]

ผลลัพธ์จะออกมาเป็น

/var/lib/docker/containers/[Container ID]/[Container ID]-json.log

ปรับเปลี่ยน format ของ Log

การจัดเก็บ Log จะถูกกำหนดรูปแบบด้วย logging driver ซึ่งโดย default เราจะใช้ json-file นอกจาก json-file เรายังสามารถเลือก logging driver ตัวอื่นๆได้ตามตารางนี้

Driver คำอธิบาย
json-file เป็นค่า default ของ docker จะเก็บ log ในรูปแบบของ JSON
syslog เขียน log messages ในรูปแบบของ syslog ซึ่งเครื่อง host ต้องมี syslog server run อยู่
journald ส่ง log messages ไปยัง journald ซึ่งเครื่อง host ต้องมี journald run อยู่
gelf ส่ง log messages ไปยัง Graylog Extended Log Format (GELF) endpoint เช่น Graylog หรือ Logstash
fluentd ส่ง log messages ไปยัง fluentd(fluentd เป็น service ที่คอยเก็บ log ไม่ใช่ log server) ซึ่งในเครื่องก็ต้องมี fluentd run อยู่
awslogs ส่ง log messages ไปยัง Amazon CloudWatch
splunk ส่ง log messages ไปยัง splunk โดยใช้ HTTP Event Collector
etwlogs ส่ง log messages เข้าไปยัง Event Tracing for Windows (ETW) เพื่อให้ไปแสดงผลใน Event Viewer ใช้ได้เฉพาะ Windows เท่านั้น
gcplogs ส่ง log messages ไปยัง Google Cloud Platform (GCP) Logging

ซึ่งเราต้องลง plug-ins เพื่อให้สามารถใช้งาน logging driver ตัวอื่นๆได้ ซึ่งวิธีการสามารถ click ที่ชื่อ logging driver ได้เลย

Phanupong Permpimol
Follow me

Software Engineer ที่เชื่อในเรื่องของ Process เพราะเมื่อ Process ดี Product ก็จะดีตาม ปัจจุบันเป็นอาจารย์และที่ปรึกษาด้านการออกแบบและพัฒนา Software และ Web Security