0

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")
2
  • 2
    If the indentation in your code correct? If so, maybe you don't get a log file because you're on windows? Commented Jun 19, 2015 at 5:39
  • 1
    Are you getting any error? which os are you using? Commented Jun 19, 2015 at 5:43

3 Answers 3

3

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")
Sign up to request clarification or add additional context in comments.

Comments

2

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")

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.