🧾 Linux/Unix: Logging Tools – journalctl, logger, and /var/log/ Explained

🧲 Introduction – Why Learn Linux Logging Tools?

Logging is crucial for Linux system administration, debugging, auditing, and security. Whether you’re a DevOps engineer or a sysadmin, tools like journalctl, logger, and files in /var/log/ help you monitor system behavior, track errors, and analyze performance issues in real-time or historically.

🎯 In this guide, you’ll learn:

  • How Linux handles system logging
  • How to query logs using journalctl
  • How to create your own logs using logger
  • How to navigate /var/log/ and its key files

πŸ“‚ 1. /var/log/ – The Traditional Log Directory

βœ… What is /var/log/?

It is the central directory where most system logs are stored in plain text. These logs are readable by tools like less, tail, or cat.


πŸ“ Common Files in /var/log/:

File NameDescription
/var/log/syslogGeneral system log (Debian/Ubuntu)
/var/log/messagesGeneral system log (RHEL/CentOS/Fedora)
/var/log/auth.logAuthentication and security events
/var/log/kern.logKernel messages
/var/log/dmesgBoot-time kernel ring buffer
/var/log/cronCron job logs
/var/log/Xorg.0.logX Window system log
/var/log/httpd/Apache web server logs

πŸ§ͺ Example: View last 50 lines of auth log

sudo tail -n 50 /var/log/auth.log

🧭 2. journalctl – View Logs from systemd Journal

βœ… What is journalctl?

journalctl is used to query logs managed by systemd, replacing traditional log rotation in newer distros like Ubuntu 20+, Fedora, RHEL 7+, and Arch.


πŸ› οΈ Syntax:

journalctl [options]

πŸ”Ή Common journalctl Examples:

βœ… View full system log:

journalctl

βœ… View logs for today:

journalctl --since today

βœ… View logs by boot:

journalctl -b

βœ… Follow logs in real-time:

journalctl -f

βœ… Show logs for a specific unit:

journalctl -u nginx.service

βœ… Limit output by time:

journalctl --since "2025-06-15 09:00" --until "2025-06-15 10:00"

🧠 Logs include system services, boot processes, user sessions, and hardware events.


πŸ“ 3. logger – Write Custom Messages to Syslog

βœ… What is logger?

logger allows users, scripts, or applications to write custom log entries to /var/log/syslog or the system journal.


πŸ› οΈ Syntax:

logger [options] "your message"

πŸ§ͺ Example 1: Add simple log

logger "Backup script completed successfully."

βœ… Appears in /var/log/syslog or journalctl.


πŸ§ͺ Example 2: Add log with tag

logger -t BACKUP "MySQL backup completed"

πŸ“€ Output in journalctl or /var/log/syslog:

Jun 15 14:02:12 hostname BACKUP: MySQL backup completed

πŸ” Tool Comparison

Feature/var/log/journalctllogger
Log SourcePlain text filessystemd journal (binary)Custom user/app logs
Rotation Supportβœ… (via logrotate)Auto-managed by systemdβœ… (sent to syslog/journal)
Real-time Viewingtail -fjournalctl -f❌ (logs via other tools)
Custom Log SupportβŒβœ…βœ… (via logger)

πŸ“Œ Summary – Recap & Next Steps

Linux logging tools help track system activity, debug errors, and maintain security logs. From the traditional /var/log/ directory to modern journalctl systems and custom logger messages, these tools provide powerful visibility into your system.

πŸ” Key Takeaways:

  • Use journalctl for querying systemd-based logs with rich filters.
  • Use logger to send script or manual messages to syslog.
  • Use /var/log/ for text-based system and service logs.

❓ FAQs

❓ Where are logs stored on systemd-based systems?
βœ… Mostly in a binary journal managed by systemd. Use journalctl to read them.

❓ How can I filter logs for a specific service?
βœ… Use:

journalctl -u sshd

❓ Can I make logger write to a custom file?
🟑 Not directly. You’ll need to configure rsyslog to redirect logs based on tags.

❓ How do I clear systemd logs?
βœ… Use:

sudo journalctl --vacuum-time=2weeks

❓ What’s the default log retention for journal logs?
🧠 It varies, but usually systemd rotates logs based on disk size and time. Configurable in /etc/systemd/journald.conf.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: Logging (journalctl, logger, /var/log/)

Or Copy Link

CONTENTS
Scroll to Top