0

I am trying out python's logger module to log info both in console and in a log and it works fine.I wanted that script to get invoked during machine star-tup and hence invoked my script from rc.local.

And my script gets invoked during boot-up, but the problem is, only console logging is working at that time. Logging to a file is not happening. Any clue on what could be the issue.

Is it anything related to syslog daemon?

import time
import logging
import datetime

global logger
global LOG_FILE

def initialize():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    time = datetime.datetime.now()
    LOG_FILE = "sample.log." + time.strftime("%Y%m%d")
    log_file_handler = logging.FileHandler(LOG_FILE)
    log_file_handler.setLevel(logging.DEBUG)
    log_file_formatter = logging.Formatter('%(message)s')
    log_file_handler.setFormatter(log_file_formatter)
    logger.addHandler(log_file_handler)

    log_file_handler = logging.StreamHandler()
    log_file_handler.setLevel(logging.INFO)
    log_file_formatter = logging.Formatter('%(message)s')
    log_file_handler.setFormatter(log_file_formatter)    
    logger.addHandler(log_file_handler)


if __name__ == '__main__':
    initialize()
    logger.info("This should go both in console and log")
    logger.debug("This should go only to Log")
3
  • 1
    Without seeing any of the code in question, no. No idea. Commented Apr 18, 2014 at 17:38
  • Have pasted the code. Like i said earlier, when i invoke this script from rc.local, only console logging works. Log file is never getting generated. Commented Apr 19, 2014 at 17:47
  • 1
    thats not how globals work. global is to be used inside of functions to reference that a variables being used is global. logger and LOG_FILE should just be set where you have global logger, then in the initialize function put global logger, LOG_FILE as the first line. Also I would suggest putting a full path to the log file, such as /var/log/sample.log, that way it doesn't matter where the script is executed from. Commented Apr 19, 2014 at 19:40

1 Answer 1

1
import time
import logging
import datetime

def initialize():
    # because logging is global module, so you can 
    # always get logger by logging.getLogger(__name__)
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    time = datetime.datetime.now()
    # if you only use LOG_FILE in function, you don't
    # have to declare it outside.
    LOG_FILE = "sample.log." + time.strftime("%Y%m%d")
    log_file_handler = logging.FileHandler(LOG_FILE)
    log_file_handler.setLevel(logging.DEBUG)
    log_file_formatter = logging.Formatter('%(message)s')
    log_file_handler.setFormatter(log_file_formatter)
    logger.addHandler(log_file_handler)

    log_file_handler = logging.StreamHandler()
    log_file_handler.setLevel(logging.INFO)
    log_file_formatter = logging.Formatter('%(message)s')
    log_file_handler.setFormatter(log_file_formatter)
    logger.addHandler(log_file_handler)


if __name__ == '__main__':
    initialize()
    logger = logging.getLogger(__name__)
    logger.info("This should go both in console and log")
    logger.debug("This should go only to Log")
Sign up to request clarification or add additional context in comments.

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.