Python 7.0pl5, Python Manual manual Logger.setLogFile/var/tmp/application-%Y-%m-%d.log

Models: Python Manual 7.0pl5

1 124
Download 124 pages 59.17 Kb
Page 15
Image 15

Specifying a Log File

The string used to specify the name of a log file may incorporate the following special tags.

Tag

Purpose

 

 

%h

Will encode the hostname of the machine into the name of the log file.

 

 

%p

Will encode the process ID into the name of the log file.

 

 

%Y

Will encode the current year as 4 digits into the name of the log file.

 

 

%y

Will encode the current year as 2 digits into the name of the log file.

 

 

%m

Will encode the current month of the year as a zero padded 2 digit

 

number into the name of the log file.

 

 

%d

Will encode the current day of month as a zero padded 2 digit number

 

into the name of the log file.

 

 

When the tags corresponding to dates are used, a new log file will automatically be created when the value corresponding to a date component changes. The following will for example result in a new log file being created each day.

logger.setLogFile("/var/tmp/application-%Y-%m-%d.log")

Note that older log files will not be removed automatically, so some other mechanism such as a cron job will need to be employed to remove them.

The name of a log file can also be set using the OTCLIB_LOGFILE environment variable instead of calling "setLogFile()". Similarly, output to the standard error output can be disabled using the OTCLIB_NOLOGSTDERR environment variable and the inclusion of the time and the process ID in the message prefix enabled using the OTCLIB_LOGLONGFORMAT environment variable. If used, these environment variables must be set before the application is run or at least before the "netsvc" module is imported for the first time.

import os

os.putenv("OTCLIB_LOGFILE","/var/tmp/application-%Y-%m-%d.log") import netsvc

When an application first attempts to open a log file, if it already exists it will be truncated. If you do not want the log file truncated, but want messages to be appended to an existing log file, the OTCLIB_APPENDLOGFILE environment variable must be set. Again, this needs to be set prior to the application being run or at least before the "netsvc" module is imported for the first time.

Note that if any of these environment variables are used, but calls are subsequently made to the corre- sponding member functions of the Logger class from within the application, the values of the envi- ronment variables will effectively be overridden from that point onwards.

15

Page 15
Image 15
Python 7.0pl5, Python Manual manual Logger.setLogFile/var/tmp/application-%Y-%m-%d.log