大网数据广告活动
服务器运维问答

How to change the date format of maillog?

· 52CE 我爱测

The format of the dates of my maillog logs looks like this: May 12 04:12:54 I need to include the year on this date. Is it possible to make this kind of change?

问题原文

原帖链接 · Diogo Braga · CC BY-SA 4.0

The format of the dates of my maillog logs looks like this:

May 12 04:12:54

I need to include the year on this date. Is it possible to make this kind of change?

社区高分解答

原帖链接 · Diogo Braga · CC BY-SA 4.0

After a little more research and some tests, I found the solution I need. My server works with CentOS 7.6, which use rsyslog to log.

  1. Edit the file /etc/rsyslog.conf and add the following template before the section "RULES":
$template MailLogFormat, "%timestamp:::date-year%-%timestamp:::date-month%-%timestamp:::date-day% %timestamp:::date-hour%:%timestamp:::date-minute%:%timestamp:::date-second% %hostname% %syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
  1. Search for the line:
# Log all the mail messages in one place.
mail.*       -/var/log/maillog

and change to:

# Log all the mail messages in one place.
mail.*       -/var/log/maillog;MailLogFormat
  1. Restart rsyslog:
systemctl restart rsyslog

Done. The logs in the /var/log/maillog file will be printed with the date in the format: Year-Month-Day Hour:Minute:Second

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-05-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。