timedatectl

centos7 中查看时区

[vagrant@localhost ~]$ timedatectl status
      Local time: Wed 2016-08-24 11:04:27 EDT
  Universal time: Wed 2016-08-24 15:04:27 UTC
        RTC time: Wed 2016-08-24 15:04:27
       Time zone: America/New_York (EDT, -0400)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2016-03-13 01:59:59 EST
                  Sun 2016-03-13 03:00:00 EDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2016-11-06 01:59:59 EDT
                  Sun 2016-11-06 01:00:00 EST

更改时区(sudo)

timedatectl set-timezone Asia/Shanghai

设置时间

timedatectl set-time '2013-06-02 23:26:00'

设置 ntp 时间同步

timedatectl set-ntp yes

官方文档
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/chap-Configuring_the_Date_and_Time.html

logrotate

添加配置文件

cat /etc/logrotate.d/myservice

/data/log/myservice/*.log
{
    rotate 7
    daily
    dateext
    dateformat .%Y%m%d
    compress
    missingok
    notifempty
    sharedscripts
    postrotate
      /usr/bin/killall -HUP rsyslogd >/dev/null 2>&1 || true
    endscript
}

debug 配置文件是否正确,但不会真正动你的 log

logrotate -d /etc/logrotate.d/myservice

真实测试 logrotate 情况

logrotate -f /etc/logrotate.d/myservice

python debug使用logging

使用 logging 有很多好处:

可以方便添加 timestamp
可以为日志定级
不会和业务的 print 混淆
如果你忘了去掉 logging ,最多也只是有一个 log 文件

如果要打印出来

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')

如果要放到文件里

import logging
logging.basicConfig(filename='log_filename.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')

报警级别

logger.critical('This is a critical message.')
logger.error('This is an error message.')
logger.warning('This is a warning message.')
logger.info('This is an informative message.')
logger.debug('This is a low-level debug message.')

http://inventwithpython.com/blog/2012/04/06/stop-using-print-for-debugging-a-5-minute-quickstart-guide-to-pythons-logging-module/