top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

LogRotation in Linux

0 votes
396 views

I am trying to use logrotate but struggling with it and not able to get the desired result. Can someone help me with some pointers or so which I can apply to rotate the files.
Any example of configuration will be great.

posted Sep 18, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
found this link http://www.thegeekstuff.com/2010/07/logrotate-examples/ looks to be complete.

1 Answer

+1 vote

Please find the code snippet below to achieve log rotation.

# **import the below packages for logging.**
import logging
import logging.handlers

# **Get a logger object by providing a logger name, which should be unique.**
logger_name = "TEST_LOG"
lgr = logging.getLogger(logger_name)

# **Create rotating file handler object. Please update the below configuration's as required.**
# **maxbytes  is the max number of bytes that can be written in a file and backupcount  is the max no of files that will be created before log rotating.** 
filepath = "/roor/test.log"
logger_mode = "W"
maxbytes = 1000000
backupcount = 10

hdlr = logging.handlers.RotatingFileHandler(filepath,mode=logger_mode,maxBytes=int(maxbytes),backupCount=int(backupcount))

# **Define the format of log statement as required.**
fmtr = logging.Formatter('%(lineno)s %(asctime)s %(levelname)s %(message)s')

# **set the format object that was created above to handler object.**
hdlr.setFormatter(fmtr)

# **set the handler object to logger object.**
lgr.addHandler(hdlr)

# **set log level to logger object. log level are explained below.**
# Level            Numeric value
# CRITICAL   50
# ERROR      40
# WARNING    30
# INFO           20
# DEBUG      10
# NOTSET       0

lgr.setLevel(logging.DEBUG)

# **You can start using the logger object for logging. For example**
lgr.debug("Good Luck !!!")

Please refer the below link for further info.
http://docs.python.org/release/2.6.8/library/logging.html?highlight=logging#module-logging

answer Sep 19, 2013 by Hemanth Anakapalle
Thanks Hemanth
Similar Questions
+1 vote

I have a problem in that at first all my log files were dated 12-31-1969 and logrotate has:

# more /var/lib/logrotate.status
logrotate state -- version 2
"/var/log/yum.log" 1969-12-31-20:26:1
"/var/named/data/named.run" 1969-12-31-20:26:1
"/var/log/httpd/error_log" 2015-8-27-4:43:1
"/var/log/wtmp" 1969-12-31-20:26:1
"/var/log/chrony/*.log" 2015-8-19-22:0:0
"/var/log/spooler" 1969-12-31-20:26:1
"/var/log/btmp" 1969-12-31-20:26:1
"/var/log/maillog" 1969-12-31-20:26:1
"/var/log/wpa_supplicant.log" 2015-8-19-22:0:0
"/var/log/secure" 1969-12-31-20:26:1
"/var/log/ppp/connect-errors" 2015-8-19-22:0:0
"/var/log/messages" 1969-12-31-20:26:1
"/var/log/cron" 1969-12-31-20:26:1
"/var/log/httpd/access_log" 2015-8-27-4:43:1

How do I get this file rebuilt with the dates currently on the files listed?

+1 vote

I'm trying to figure out a way to insert a timestamp into a log file I'm creating for a cron job:

/usr/bin/rsync -v --min-size=1 -rlpgo -O --inplace /home/myuser/Get*
root@xx.x.xx.xx:/mnt/yyy/zzz/compass 2>&1 >& /tmp/rsync_user.log

I've googled this but all I could find are tips on how to add the time and date to the name of the log file itself, not to the contents of the log.

Any tips or pointers would be great.

0 votes

The system log (/var/log/messages) of a CentOS 7.2 system has frequently-repeated message line pairs like:

Jul 18 14:00:01 localhost systemd: Started Session 307 of user root.
Jul 18 14:00:01 localhost systemd: Starting Session 307 of user root.

where the session number increases each time.

Looking around on this, e.g. Red Hat Bugzilla bug 727315, it looks like it's when crond starts a task; it looks like it might be fixed - I would think that would be in CentOS but don't know how to find/compare the Fedora and CentOS systemd versions to know for sure.

I found a post on a workaround - in /etc/systemd/system.conf to change the line:

#LogLevel=info

to:

LogLevel=notice

I did that and rebooted, and it has stopped the messages.

I'm worried though that this may have knocked out something of actual interest from the syslog.

So my question is, is there a better way? A way that info messages could go to some other log, or better yet, a way that those particular "session" messages, and only those, could go to some other log or be filtered out?

+1 vote

I am trying to figure out how to forward mainly the Error Logs from an Apache for Windows installation over to a remote syslog server. All documents I can locate show how to do this if Apache is running in Linux. Does anyone know how to make this happen in Windows?

+1 vote

I recently upgraded my Subversion client from version 1.7.5 to 1.8.3 and from that time on I experience hangs when executing multiple 'svn log' commands in a short amount of time. The "hanging" svn process consumes all CPU cycles it gets but never finishes.

I can reproduce the problem using the following shell script (on Windows using Cygwin):

#!/bin/bash
ROOT="http://example.com/svn/trunk"
for DIR in $(svn ls "${ROOT}" | head -n 4); do
 svn log -v -l 5 "${ROOT}/${DIR}" 2>&1 | head -n 100 > /dev/null
done

In my environment I need at least 4 svn processes to achieve the faulty behavior. The hang only occurs if the output is truncated using the head command (but it can be redirected to a normal file instead of /dev/null which then contains the expected log messages).

...