Python File logger also logging to stdout

Asked
Active3 hr before
Viewed126 times

7 Answers

loggerstdoutpythonlogging
90%

I'm using Python's logging module to log some debug strings to a file which works pretty well, Now in addition, I'd like to use this module to also print the strings out to stdout

Example_snippet/controller/utility/_logger.js/ import logging import logging.. . .
import logging
import logging.handlers
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
   LOGFILE, maxBytes = (1048576 * 5), backupCount = 7
)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
Step 2 continued with logger.debug("I am written to . . .
logger.debug("I am written to the file")
load more v
88%

The handler is the engine that determines what happens to each message in a logger, It describes a particular logging behavior, such as writing a message to the screen, to a file, or to a network socket

Example_snippet/controller/utility/_logger.js/ # import the logging library i. . .
#
import the logging library
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)

def my_view(request, arg1, arg):
   ...
   if bad_mojo:
   # Log an error message
logger.error('Something went wrong!')
load more v
72%

You can also specify the logging level for the log file by passing --log-file-level, This setting accepts the logging level names as seen in python’s documentation(ie, uppercased level names) or an integer as the logging level num

Example_snippet/controller/utility/_logger.js/ pytest . . .
pytest
load more v
65%

This example also shows how you can set the logging level which acts as the threshold for tracking, In this case, because we set the threshold to DEBUG, all of the messages were printed

Example_snippet/controller/utility/_logger.js/ import logging logging.warning. . .
import logging
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
load more v
75%

Meta Stack Overflow , Stack Overflow help chat ,Stack Overflow en español,Stack Overflow на русском

Example_snippet/controller/utility/_logger.js/ import logging import sys roo. . .
import logging
import sys

root = logging.getLogger()
root.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
load more v
40%

Example_snippet/controller/utility/_logger.js/ import logging import sys log. . .
import logging
import sys

logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(message)s',
   '%m-%d-%Y %H:%M:%S')

stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.DEBUG)
stdout_handler.setFormatter(formatter)

file_handler = logging.FileHandler('logs.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)

logger.addHandler(file_handler)
logger.addHandler(stdout_handler)
load more v
22%

Log to stdout With the logging,StreamHandler() Function in Python,Logging to stdout in Python,Log to stdout With the logging

Example_snippet/controller/utility/_stdout.js/ import logging import sys #Cr. . .
import logging
import sys

#Creating and Configuring Logger

Log_Format = "%(levelname)s %(asctime)s - %(message)s"

logging.basicConfig(filename = "logfile.log",
   stream = sys.stdout,
   filemode = "w",
   format = Log_Format,
   level = logging.ERROR)

logger = logging.getLogger()

#Testing our Logger

logger.error("Our First Error Message")
load more v