Python Forum
Properly share logger objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Properly share logger objects
#1
Hi
In python file 1 I have

 
      def main():
       
           logger = logging.getLogger("pre-commit-logger")
           handler = logging.FileHandler(log_file)
           logger.addHandler(handler)
           logger.warning("logger warn")
           .......
The warning message is correctly added to the log file

In file 2 I have

 
   logger = logging.getLogger("pre-commit-logger")

   logger.warning("executing asdasdasdasdasdasdasdas!!!")
When I execute the script I get

   No handlers could be found for logger "pre-commit-logger"

I have obviously misunderstood something and qwould be grateful of an expanation

Thanks
Reply
#2
How are these files related? Could you provide complete code that reproduces the issue?
Reply
#3
(Jun-26-2017, 10:07 AM)CardBoy Wrote: Hi
In python file 1 I have

 
      def main():
       
           logger = logging.getLogger("pre-commit-logger")
           handler = logging.FileHandler(log_file)
           logger.addHandler(handler)
           logger.warning("logger warn")
           .......
The warning message is correctly added to the log file

In file 2 I have

 
   logger = logging.getLogger("pre-commit-logger")

   logger.warning("executing asdasdasdasdasdasdasdas!!!")
When I execute the script I get

   No handlers could be found for logger "pre-commit-logger"

I have obviously misunderstood something and qwould be grateful of an expanation

Thanks

You may be assuming that like in Java, the logger is a singleton and that you don't need to add the handler to the second instance. But it's easy to check if the two references are the same object or not. In a sane world, the handlers are defined in a logger configuration file anyway.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Timestamp of file changes if a share mapped to a server…. tester_V 34 3,631 Jul-04-2023, 05:19 AM
Last Post: tester_V
  logger behaviour setdetnet 1 853 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  how to write exception error into logger mg24 3 948 Nov-15-2022, 04:20 PM
Last Post: insharazzak
  python insert blank line in logger mg24 1 2,724 Nov-02-2022, 08:36 AM
Last Post: snippsat
  python logger help ... save logger into different folder mg24 1 1,350 Oct-25-2022, 03:04 PM
Last Post: snippsat
  Closing logger from other function problem Paqqno 1 1,082 Apr-25-2022, 11:49 AM
Last Post: Gribouillis
  Closing logger to rename directory malcoverc 1 1,175 Apr-19-2022, 07:06 AM
Last Post: Gribouillis
  access share attributed among several class methods drSlump 0 1,038 Nov-18-2021, 03:02 PM
Last Post: drSlump
  logger option , where is the file? korenron 1 1,756 Apr-25-2021, 01:28 PM
Last Post: snippsat
  How to share a numpy array between 2 processes on Windows? qstdy 0 2,135 Jan-29-2021, 04:24 AM
Last Post: qstdy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020