I have Django 1.8.17 and I can see django-debug-toolbar from my dashboard.
I'd like to be able to log messages in Logging panel\
setting.py
import logging
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
I can see output in Terminal console but not in Logging panel
Any clue?
Related
I created a my_logger class as below:
import logging
import logging.handlers
import os
abspath = os.path.abspath(os.path.dirname(__file__))
format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(lineno)d - %(message)s')
def my_logger(module_name):
logger = logging.getLogger(module_name)
logger.setLevel(logging.DEBUG)
# stream handler
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.DEBUG)
c_handler.setFormatter(format)
# debug handler
f1_handler = logging.handlers.RotatingFileHandler(os.path.join(abspath, "logs/debug.log"))
f1_handler.setLevel(logging.DEBUG)
f1_handler.setFormatter(format)
# info handler
f2_handler = logging.handlers.RotatingFileHandler(os.path.join(abspath, "logs/info.log"))
f2_handler.setLevel(logging.INFO)
f2_handler.setFormatter(format)
# warning handler
f3_handler = logging.handlers.RotatingFileHandler(os.path.join(abspath, "logs/warning.log"))
f3_handler.setLevel(logging.WARNING)
f3_handler.setFormatter(format)
# error handler
f4_handler = logging.handlers.RotatingFileHandler(os.path.join(abspath, "logs/error.log"))
f4_handler.setLevel(logging.ERROR)
f4_handler.setFormatter(format)
# Add handlers to the logger
logger.addHandler(c_handler)
logger.addHandler(f1_handler)
logger.addHandler(f2_handler)
logger.addHandler(f3_handler)
logger.addHandler(f4_handler)
return logger
Then in my code, I used it in any python file this way:
logger = my_logger(__name__)
logger.warning('This is a warning')
logger.error('This is an error')
logger.info('This is an info')
logger.debug('This is an debug')
How to turn off and on in production and development mode in the project? I have never done this before.
I would like my logger func to show my comments to both console (screen) and file. Doing below only open and update a file. What am I missing?
def get_logger():
# create logger
logger = logging.getLogger('my_project')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('logger.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
return logger
In my main py:
logger = logger.get_logger()
def get_logger():
# create logger
logger = logging.getLogger('my_project')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('logger.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
return logger
Have this part of code:
import logging
logFormatter = logging.Formatter(u'#%(levelname)-8s [%(asctime)s] [LINE:%(lineno)d] %(filename)-8s %(message)s',)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
logger.addHandler(consoleHandler)
How I can handle logging.error calling, for example to save log in database or do something else
Probably the simplest answer is to use multiple handlers, setup with different logging levels. From the logging docs, here is a perfect example of what you are trying to achieve:
import logging
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
The example provides both the DEBUG and ERROR level handler formatting for these log level types.
Perhaps another solution is to switch to using dictConfig for handling the python logging module configuration. That would handle the structure in a dictionary type way, and you may be able to provide multiple values to the level attribute.
I'm trying to figure out how to capture messages generated by Python/NumPy intractive shell when running my script. I would like to log all generated by console messages (errors, warnings) to same file as defined in my code log messages with time stamps:
def LogToFile():
global logger
logger = logging.getLogger('MyApp')
logger.setLevel(logging.DEBUG)
file_log_handler = RotatingFileHandler('logfile.log', maxBytes=1024, backupCount=5)
logger.addHandler(file_log_handler)
stderr_log_handler = logging.StreamHandler()
logger.addHandler(stderr_log_handler)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_log_handler.setFormatter(formatter)
stderr_log_handler.setFormatter(formatter)
return logger
Afaik, you'd have to specify this in basicConfig, not in your logger:
logging.basicConfig(filename=LOG_FILE,
level=logging.DEBUG)
before you do
logger = logging.getLogger('MyApp')
logger.setLevel(logging.DEBUG)
I'm trying to use a basic python (v2.7.5) logger:
import logging
TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S"
LOG_FORMAT = "%(asctime)s [%(levelname)s] %(message)s"
LOG_PATH = "/tmp/exaple.log"
logging.basicConfig(
format=LOG_FORMAT,
level=logging.DEBUG,
filename=LOG_PATH,
datefmt=TIMESTAMP_FORMAT
)
logger = logging.getLogger("exaple")
logger.error("example")
As you can see, 'level' is set to logging.DEBUG, the line appears at example.log as expected, yet nothing is printed! Why?
Because you set 1 handler, here is multiple handlers example
import logging
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('/tmp/spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
https://docs.python.org/2/howto/logging-cookbook.html#multiple-handlers-and-formatters