Tuesday 14 February 2012

Truncating, Rotating, Flushing Listener.log file

Truncating Listener log
     # Check the current size of listener log
     $ ls -l
     -rw-r----- 1 oracle dba 9756946 Jun 27 11:01 listener.log
     # Make backup of the file
     $ zip listener.log.zip listener.log
     adding: listener.log (deflated 93%)
     # Truncate file 
     $ cat /dev/null > listener.log
     # Check the size
     $ ls -l
     total 672
     -rw-r----- 1 oracle dba      0 Jun 27 11:07 listener.log
     -rw-r--r-- 1 oracle dba 682614 Jun 27 11:05 listener.log.zip
You could also use [ $echo "" > listener.log ] to truncate file.

Using LSNRCTL utility:
     ### CURRENT LISTENER LOG FILE
     $ ls -l
     -rw-r----- 1 oracle oinstall 3532 Jun 27 15:36 listener.log
     ### STARTING LSNRCTL UTILITY
     $ lsnrctl
     LSNRCTL for Linux: Version 10.2.0.4.0 - Production on 27-JUN-2009 15:37:53
     Copyright (c) 1991, 2007, Oracle.  All rights reserved.
     Welcome to LSNRCTL, type "help" for information.
     ### SETTING CURRENT LISTENER
     LSNRCTL> set current_listener LISTENER
     Current Listener is LISTENER
     ### CHANGING CURRENT LISTENER LOG LOCATION TO TEMPORARY
     LSNRCTL> set log_file listener_temp
     Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=00.0.00.18)(PORT=1525))
     LISTENER parameter "log_file" set to listener_temp.log
     The command completed successfully
     ### EXIT
     LSNRCTL> exit
     ### WE CREATED NEW LISTENER LOG FILE listener_temp.log (TEMPORARY)
     $ ls -l
     -rw-r----- 1 oracle oinstall 3532 Jun 27 15:36 listener.log
     -rw-r----- 1 oracle oinstall   36 Jun 27 15:38 listener_temp.log
          ### BACKUP OLD LISTENER LOGS
     $ mv listener.log listener.log.bak
          ### START LSNRCTL UTILITY
     $ lsnrctl
     LSNRCTL for Linux: Version 10.2.0.4.0 - Production on 27-JUN-2009 15:39:25
     Copyright (c) 1991, 2007, Oracle.  All rights reserved.
     Welcome to LSNRCTL, type "help" for information.
     ### RENAME LISTENER LOG BACK TO listener.log
     LSNRCTL> set log_file listener
     Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=00.0.00.18)(PORT=1525))
     LISTENER parameter "log_file" set to listener.log
     The command completed successfully
     LSNRCTL> exit
     ### WE NOW HAVE NEW TRUNCATED listener.log AND OLD LOGS Backupedup
     $ ls -l
     -rw-r----- 1 oracle oinstall   36 Jun 27 15:39 listener.log
     -rw-r----- 1 oracle oinstall 3532 Jun 27 15:36 listener.log.bak
     -rw-r----- 1 oracle oinstall   36 Jun 27 15:38 listener_temp.log

Using ADR Utility:
Oracle 11g introduced new diagnostically system - "Automatic Diagnostic Repository (ADR)" and now we have listener log written to xml file (besides listener.log file).
Trying to change log file using LSNRCTL utility will fail with:
TNS-01251: Cannot set trace/log directory under ADR.
LSRNCTL>set current_listener
LSNRCTL>set log_directory fails:
TNS-01251: Cannot set trace/log directory under ADR


Using LOGROTATE command:
We can use log rotate command in cron to rotate listener logs in some time intervals. Logrotate is designed to help administrators with managing large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files.
Simple logrotate configuration:
Add file:
/etc/logrotate.d/oracle
- write this lines into that file:
/oracle/product/10.2.0/network/log/listener.log {
missingok
weekly
rotate 4
compress
create 644 oracle dba
}

Logs will rotate every week and old ones will be compressed. We will have 4 latest backups.

Fourth solution - using LSNRCTL utility
Oracle TNS listener holds an open handle to listener.log file so I can't just rename file because Oracle won't create new log file for logging purposes. 
To recreate log file we must restart TNS listener process. Better option is to disable logging to release open handle, rename file and enable logging.
     $ cd $ORACLE_HOME/network/log
     $ lsnrctl set log_status off
     $ mv listener.log listener.old
     $ lsnrctl set log_status on

No comments:

Post a Comment