Personal Blog

devops

Journal日志持久化

配置Journal,使其日志持久化

Journal日志持久化是什么?

Linux Journal 日志(由 systemd-journald 服务管理)是随着 systemd 系统初始化框架(2010 年后逐步成为主流 Linux 系统的默认初始化系统)出现的新一代日志管理系统, 旨在替换传统 Linux syslog日志系统,有以下几个有点: 1. 传统 syslog 依赖文本文件存储,分散在/var/log下(如messages、auth.log等),缺乏统一管理,且易受权限、磁盘空间影响。 2. 日志格式不统一,不同程序日志格式差异大,不利于检索和分析。 3. 缺乏结构化数据支持,难以高效过滤和关联日志信息。

systemd-journald 作为 systemd 的核心组件,通过二进制格式存储日志,提供了更高效、统一的日志管理方案,现已成为多数主流 Linux 发行版(如 Fedora、Debian 9+、Ubuntu 16.04+、CentOS 7+)的默认日志系统。

如何配置

默认日志存储在/run/log/journal,该日志从位置也可以看出,会随着机器重启而丢失。 所以需要配置/var/log/journal实现持久化,同时支持日志轮转和大小限制等,避免磁盘占满。

  1. 提前创建持久化所在的目录

     mkdir -p /var/log/journal
    
  2. 修改配置文件/etc/systemd/journald.conf如下

     cat > /etc/systemd/journald.conf << 'EOF'
     #  This file is part of systemd.
     #
     #  systemd is free software; you can redistribute it and/or modify it under the
     #  terms of the GNU Lesser General Public License as published by the Free
     #  Software Foundation; either version 2.1 of the License, or (at your option)
     #  any later version.
     #
     # Entries in this file show the compile time defaults. Local configuration
     # should be created by either modifying this file, or by creating "drop-ins" in
     # the journald.conf.d/ subdirectory. The latter is generally recommended.
     # Defaults can be restored by simply deleting this file and all drop-ins.
     #
     # Use 'systemd-analyze cat-config systemd/journald.conf' to display the full config.
     #
     # See journald.conf(5) for details.
    
     [Journal]
     Storage=persistent
     SystemMaxFileSize=500M
     SystemMaxFiles=10
     SystemMaxUse=5G
     #Compress=yes
     #Seal=yes
     #SplitMode=uid
     #SyncIntervalSec=5m
     #RateLimitIntervalSec=30s
     #RateLimitBurst=10000
     #SystemKeepFree=
     #RuntimeMaxUse=
     #RuntimeKeepFree=
     #RuntimeMaxFileSize=
     #RuntimeMaxFiles=100
     #MaxRetentionSec=
     #MaxFileSec=1month
     #ForwardToSyslog=no
     #ForwardToKMsg=no
     #ForwardToConsole=no
     #ForwardToWall=yes
     #TTYPath=/dev/console
     #MaxLevelStore=debug
     #MaxLevelSyslog=debug
     #MaxLevelKMsg=notice
     #MaxLevelConsole=info
     #MaxLevelWall=emerg
     #LineMax=48K
     #ReadKMsg=yes
     #Audit=yes
     EOF
    
  3. 重启journald服务

     systemctl restart systemd-journald
     systemctl status systemd-journald
    

DEVOPS · LINUX
devops linux