Python define logger in one location without using basic config or file config












1














The question of how to define a logger in one location was asked here:
Using Python logging in multiple modules



However the two top answers use basic config or a file config. I don't want to use a file config because I want to be able to dynamically adjust the level with command line arguments. I don't want to use a basic config because, well, my config won't be basic.



This is not library code so I'm not concerned with whatever might happen if the module is imported. My code also has a single entry point.



This is (a simplified) version of what I have at the moment:



temp1.py :



import logging
import sys
import temp2

formatter = logging.Formatter(
fmt = '%(asctime)s - %(module)s - %(levelname)s ===> %(message)s'
)
logger = logging.getLogger('root')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info("THIS IS AN INFO MESSAGE IN TEMP 1")

temp2.do_something()


temp2.py:



import logging

logger = logging.getLogger(__name__)

logger.info('In temp 2')

def do_something():
logger.critical('doing someting in temp 2')


This is the output:



2018-11-23 18:31:40,543 - temp1 - INFO ===> THIS IS AN INFO MESSAGE IN TEMP 1
doing something in temp 2


The output I desire would:
1) Format the output in the file temp2.py as well and
2) Also log the module_level logging message in temp2.py



I've been through the docs but I can't figure this one out. What do I need to do?










share|improve this question






















  • Couldn't the file config be set to some default level that you then override with your command line parameters, i.e. read the file config then read command line parameters, overriding as necessary?
    – Jonah Bishop
    Nov 23 '18 at 16:41












  • Maybe, but I then don't see how that override would propagate through to the other files. So I'd still be in the same position.
    – Neil
    Nov 23 '18 at 16:43






  • 1




    Do you have control over the other modules? One method I've used is passing the logger itself into the other modules as arguments. I concede it doesn't feel very pythonic but it does work.
    – Idlehands
    Nov 23 '18 at 16:56












  • I could do that I suppose.
    – Neil
    Nov 23 '18 at 16:57






  • 1




    Another way is you can separate the logger itself entirely as a separate module, and then in each of your module from my_logger import logger. You might even try: logger.debug(f'In {__name__}'); except NameError: from my_logger import logger; to check if a logger already exists, if not, load in your custom logger
    – Idlehands
    Nov 23 '18 at 17:00


















1














The question of how to define a logger in one location was asked here:
Using Python logging in multiple modules



However the two top answers use basic config or a file config. I don't want to use a file config because I want to be able to dynamically adjust the level with command line arguments. I don't want to use a basic config because, well, my config won't be basic.



This is not library code so I'm not concerned with whatever might happen if the module is imported. My code also has a single entry point.



This is (a simplified) version of what I have at the moment:



temp1.py :



import logging
import sys
import temp2

formatter = logging.Formatter(
fmt = '%(asctime)s - %(module)s - %(levelname)s ===> %(message)s'
)
logger = logging.getLogger('root')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info("THIS IS AN INFO MESSAGE IN TEMP 1")

temp2.do_something()


temp2.py:



import logging

logger = logging.getLogger(__name__)

logger.info('In temp 2')

def do_something():
logger.critical('doing someting in temp 2')


This is the output:



2018-11-23 18:31:40,543 - temp1 - INFO ===> THIS IS AN INFO MESSAGE IN TEMP 1
doing something in temp 2


The output I desire would:
1) Format the output in the file temp2.py as well and
2) Also log the module_level logging message in temp2.py



I've been through the docs but I can't figure this one out. What do I need to do?










share|improve this question






















  • Couldn't the file config be set to some default level that you then override with your command line parameters, i.e. read the file config then read command line parameters, overriding as necessary?
    – Jonah Bishop
    Nov 23 '18 at 16:41












  • Maybe, but I then don't see how that override would propagate through to the other files. So I'd still be in the same position.
    – Neil
    Nov 23 '18 at 16:43






  • 1




    Do you have control over the other modules? One method I've used is passing the logger itself into the other modules as arguments. I concede it doesn't feel very pythonic but it does work.
    – Idlehands
    Nov 23 '18 at 16:56












  • I could do that I suppose.
    – Neil
    Nov 23 '18 at 16:57






  • 1




    Another way is you can separate the logger itself entirely as a separate module, and then in each of your module from my_logger import logger. You might even try: logger.debug(f'In {__name__}'); except NameError: from my_logger import logger; to check if a logger already exists, if not, load in your custom logger
    – Idlehands
    Nov 23 '18 at 17:00
















1












1








1







The question of how to define a logger in one location was asked here:
Using Python logging in multiple modules



However the two top answers use basic config or a file config. I don't want to use a file config because I want to be able to dynamically adjust the level with command line arguments. I don't want to use a basic config because, well, my config won't be basic.



This is not library code so I'm not concerned with whatever might happen if the module is imported. My code also has a single entry point.



This is (a simplified) version of what I have at the moment:



temp1.py :



import logging
import sys
import temp2

formatter = logging.Formatter(
fmt = '%(asctime)s - %(module)s - %(levelname)s ===> %(message)s'
)
logger = logging.getLogger('root')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info("THIS IS AN INFO MESSAGE IN TEMP 1")

temp2.do_something()


temp2.py:



import logging

logger = logging.getLogger(__name__)

logger.info('In temp 2')

def do_something():
logger.critical('doing someting in temp 2')


This is the output:



2018-11-23 18:31:40,543 - temp1 - INFO ===> THIS IS AN INFO MESSAGE IN TEMP 1
doing something in temp 2


The output I desire would:
1) Format the output in the file temp2.py as well and
2) Also log the module_level logging message in temp2.py



I've been through the docs but I can't figure this one out. What do I need to do?










share|improve this question













The question of how to define a logger in one location was asked here:
Using Python logging in multiple modules



However the two top answers use basic config or a file config. I don't want to use a file config because I want to be able to dynamically adjust the level with command line arguments. I don't want to use a basic config because, well, my config won't be basic.



This is not library code so I'm not concerned with whatever might happen if the module is imported. My code also has a single entry point.



This is (a simplified) version of what I have at the moment:



temp1.py :



import logging
import sys
import temp2

formatter = logging.Formatter(
fmt = '%(asctime)s - %(module)s - %(levelname)s ===> %(message)s'
)
logger = logging.getLogger('root')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info("THIS IS AN INFO MESSAGE IN TEMP 1")

temp2.do_something()


temp2.py:



import logging

logger = logging.getLogger(__name__)

logger.info('In temp 2')

def do_something():
logger.critical('doing someting in temp 2')


This is the output:



2018-11-23 18:31:40,543 - temp1 - INFO ===> THIS IS AN INFO MESSAGE IN TEMP 1
doing something in temp 2


The output I desire would:
1) Format the output in the file temp2.py as well and
2) Also log the module_level logging message in temp2.py



I've been through the docs but I can't figure this one out. What do I need to do?







python logging module






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 16:34









Neil

55019




55019












  • Couldn't the file config be set to some default level that you then override with your command line parameters, i.e. read the file config then read command line parameters, overriding as necessary?
    – Jonah Bishop
    Nov 23 '18 at 16:41












  • Maybe, but I then don't see how that override would propagate through to the other files. So I'd still be in the same position.
    – Neil
    Nov 23 '18 at 16:43






  • 1




    Do you have control over the other modules? One method I've used is passing the logger itself into the other modules as arguments. I concede it doesn't feel very pythonic but it does work.
    – Idlehands
    Nov 23 '18 at 16:56












  • I could do that I suppose.
    – Neil
    Nov 23 '18 at 16:57






  • 1




    Another way is you can separate the logger itself entirely as a separate module, and then in each of your module from my_logger import logger. You might even try: logger.debug(f'In {__name__}'); except NameError: from my_logger import logger; to check if a logger already exists, if not, load in your custom logger
    – Idlehands
    Nov 23 '18 at 17:00




















  • Couldn't the file config be set to some default level that you then override with your command line parameters, i.e. read the file config then read command line parameters, overriding as necessary?
    – Jonah Bishop
    Nov 23 '18 at 16:41












  • Maybe, but I then don't see how that override would propagate through to the other files. So I'd still be in the same position.
    – Neil
    Nov 23 '18 at 16:43






  • 1




    Do you have control over the other modules? One method I've used is passing the logger itself into the other modules as arguments. I concede it doesn't feel very pythonic but it does work.
    – Idlehands
    Nov 23 '18 at 16:56












  • I could do that I suppose.
    – Neil
    Nov 23 '18 at 16:57






  • 1




    Another way is you can separate the logger itself entirely as a separate module, and then in each of your module from my_logger import logger. You might even try: logger.debug(f'In {__name__}'); except NameError: from my_logger import logger; to check if a logger already exists, if not, load in your custom logger
    – Idlehands
    Nov 23 '18 at 17:00


















Couldn't the file config be set to some default level that you then override with your command line parameters, i.e. read the file config then read command line parameters, overriding as necessary?
– Jonah Bishop
Nov 23 '18 at 16:41






Couldn't the file config be set to some default level that you then override with your command line parameters, i.e. read the file config then read command line parameters, overriding as necessary?
– Jonah Bishop
Nov 23 '18 at 16:41














Maybe, but I then don't see how that override would propagate through to the other files. So I'd still be in the same position.
– Neil
Nov 23 '18 at 16:43




Maybe, but I then don't see how that override would propagate through to the other files. So I'd still be in the same position.
– Neil
Nov 23 '18 at 16:43




1




1




Do you have control over the other modules? One method I've used is passing the logger itself into the other modules as arguments. I concede it doesn't feel very pythonic but it does work.
– Idlehands
Nov 23 '18 at 16:56






Do you have control over the other modules? One method I've used is passing the logger itself into the other modules as arguments. I concede it doesn't feel very pythonic but it does work.
– Idlehands
Nov 23 '18 at 16:56














I could do that I suppose.
– Neil
Nov 23 '18 at 16:57




I could do that I suppose.
– Neil
Nov 23 '18 at 16:57




1




1




Another way is you can separate the logger itself entirely as a separate module, and then in each of your module from my_logger import logger. You might even try: logger.debug(f'In {__name__}'); except NameError: from my_logger import logger; to check if a logger already exists, if not, load in your custom logger
– Idlehands
Nov 23 '18 at 17:00






Another way is you can separate the logger itself entirely as a separate module, and then in each of your module from my_logger import logger. You might even try: logger.debug(f'In {__name__}'); except NameError: from my_logger import logger; to check if a logger already exists, if not, load in your custom logger
– Idlehands
Nov 23 '18 at 17:00














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450220%2fpython-define-logger-in-one-location-without-using-basic-config-or-file-config%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450220%2fpython-define-logger-in-one-location-without-using-basic-config-or-file-config%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)