code
I am using logging module to create log file.It checks the platform and provide method to create logfile as per the platform
import os,platform,logging
if platform.platform('windows'):
logging_file=os.path.join(os.getenv('HOMEDRIVE'),os.getenv('HOMEPATH'),'test.log')
else:
logging_file = os.path.join(os.getenv('HOME'),'test.log')
print"logging to",logging_file
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s : %(levelname)s :%(message)s',filename=logging_file, filemode='w')
logging.DEBUG("start of the program")
logging.info("doing something")
logging.warning("u are gonna die")
The code you provided is not indented properly and you should use logging.debug
instead of logging.DEBUG
since the later is an Integer constant used to represent log level. Here is the updated code
import os,platform,logging
if platform.platform('windows'):
logging_file=os.path.join(os.getenv('HOMEDRIVE'),os.getenv('HOMEPATH'),'test.log')
else:
logging_file = os.path.join(os.getenv('HOME'),'test.log')
print"logging to",logging_file
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s : %(levelname)s :%(message)s',
filename=logging_file,
filemode='w')
logging.debug("start of the asdfasdfprogram")
logging.info("doing something")
logging.warning("u are gonna die")
Maybe you are using a unix system , the below statement is wrong -
platform.platform('windows')
The platform.platform
function always returns the underlying platform as a string, later on you are directly testing the string
in if condition, which will always return true, hence it always assumes you are in windows system.
You need a condition like -
if 'windows' in platform.platform().lower():
Also, as k4vin said - if this isn't a type you should be using logging.debug
instead of logging.DEBUG
, the first is a function, where as the second is the integer used to indicate the logging level DEBUG
.
logging.debug("start of the program")
The logging handlers should be cleaned before creating the basicconfig
Code
import os,platform,logging
if 'windows' in platform.platform().lower():
logging_file=os.path.join(os.getenv('HOMEDRIVE'),os.getenv('HOMEPATH'),'test.log')
else:
logging_file = os.path.join(os.getenv('HOME'),'test.log')
print"logging to",logging_file
logging.getLogger('').handlers = []
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s : %(levelname)s :%(message)s',filename=logging_file, filemode='w')
logging.DEBUG("start of the program")
logging.info("doing something")
logging.warning("u are gonna die")
you could view this link for more information
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.