0
logger = logging.getLogger('application')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('app.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

class blah()

    def blah1(self)
        ....
        ....
        self.logger.DEBUG("You dont have root privileges")

    def blah1(self)
        ....
        ....
        self.logger.DEBUG("You dont have root privileges2")


if __name__ == "__main__":
    b= blah()
    b.blah1()

Guys im trying to get each of my functions to report into a log file in my python script. But its not logging. Where have i gone wrong?

Thanks William

1
  • self.logger.DEBUG("You dont have root privileges") , can you please show the line where you have defined logger in class Commented Jan 30, 2014 at 9:41

2 Answers 2

2

Find the simplified answer

import logging
logger = logging.getLogger('application')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('app.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

class blah:
    def blah1(self):
        logger.debug("You dont have root privileges")


if __name__ == "__main__":
    b= blah()
    b.blah1()

NOTE: debug should be small case, and please used global logger. as you are initializing it in global scope

Sign up to request clarification or add additional context in comments.

Comments

1

try

class blah():
    logger = logger

    def blah1(self)
        ....
        ....
        self.logger.DEBUG("You dont have root privileges")

    def blah1(self)
        ....
        ....
        self.logger.DEBUG("You dont have root privileges2")

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.