simple introduction to
In version 2.6 of
It hides complexieties in your application initiation. That is the above code would be something like below without using
As you can see
To add
By setting
#python #cement #framework #logging #log #level #foundation
Cement
framework and its usage. Cement
framework is mostly used for creating a command line application using python
.In version 2.6 of
Cement
you can initiate an app using with
:from cement.core.foundation import CementApp
with CementApp('myapp') as app:
app.run()
It hides complexieties in your application initiation. That is the above code would be something like below without using
with
:from cement.core.foundation import CementApp
app = CementApp('myapp')
app.setup()
app.run()
app.close()
As you can see
with
procedure is more clear and straight forward with less code. I know it's silly to have an app like above, but that's just an introduction to the world of Cement
.To add
logger
to your framework you need to set log level in log.logging
as below:from cement.utils.misc import init_defaults
from cement.core.foundation import CementApp
defaults = init_defaults('myapp', 'log.logging')
defaults['log.logging']['level'] = 'DEBUG'
defaults['log.logging']['file'] = 'cementy.log'
defaults['log.logging']['to_console'] = True
with CementApp('myapp', config_defaults=defaults) as app:
app.run()
app.log.debug('This is debug')
init_defaults
is used to setup logging. level
sets the log level to DEBUG
. file
would write log data into cementy.log
file.By setting
to_console
param you can also write the data written to file into console too. So if you run your python application, a file would be created for logging and data will be printed out.#python #cement #framework #logging #log #level #foundation
How to add color to your logs in
It's easy as pie, just install
By default the
If you don't want to see log messages from libraries, you can pass a specific logger object to the
#log #logger #coloredlogs #logging #color
python
?It's easy as pie, just install
coloredlogs
with pip and then:import coloredlogs, logging
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG')
# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")
By default the
install()
function installs a handler on the root logger, this means that log messages from your code and log messages from the libraries that you use will all show up on the terminal.If you don't want to see log messages from libraries, you can pass a specific logger object to the
install()
function. In this case only log messages originating from that logger will show up on the terminal:coloredlogs.install(level='DEBUG', logger=logger)
#log #logger #coloredlogs #logging #color
What is filters in
Filter is a way to tell logger not to log or to log something. Let's say you print sensitive data in a core module that can also handle passwords. Here you don't want to record user passwords in you logs! Do you?!
The above method will not log lines which start with
to not record them or mask the password part by chaning the the log using:
Just get your message using the above line and replace it with something you want.
#python #logging #filter
Python Logging
and what we can do with it?Filter is a way to tell logger not to log or to log something. Let's say you print sensitive data in a core module that can also handle passwords. Here you don't want to record user passwords in you logs! Do you?!
class NoPasswordFilter(logging.Filter):
def filter(self, record):
return not record.getMessage().startswith('password')
logger.addFilter(NoPasswordFilter())
The above method will not log lines which start with
password
. You can use regex to find your template in you logs and return falseto not record them or mask the password part by chaning the the log using:
record.msg
Just get your message using the above line and replace it with something you want.
#python #logging #filter
Do you log a lot like me in your
You can see that both have the same log content but it's hard to follow
The formatter is as below:
Now the output will something like below:
You can see that log content is so much easier to follow by using space padding. It may not be obvious on telegram with small devices. So try it your self :)))
#python #logging #log #logger #formatter #log_formatter #space_padding #padding
Python
modules? If so, you had the same problem to always find the first occurence of a log after time, filename, etc. Let's clarify this with a sample log:[2012-10-02 application.py:1 _get()] DEBUG: this is a log content
[2012-10-02 db.py:1005 _fetch_all_info()] INFO: this is a log content
You can see that both have the same log content but it's hard to follow
cause of length of file name, line number and function name. To format this better we can have space padding in formatters. spaces are identified by `s
. Now lets see the same log, but this time with space padding.The formatter is as below:
[%(asctime)s %(filename)15s:%(lineno)4s %(funcName)20s()] %(levelname)s %(message)s
NOTE: this is not the exact formatter for the above log, it is for demonstration!
Now the output will something like below:
[2012-10-02 application.py: 1 _get()] DEBUG: this is a log content
[2012-10-02 db.py: 1005 _fetch_all_info()] DEBUG: this is a log content
You can see that log content is so much easier to follow by using space padding. It may not be obvious on telegram with small devices. So try it your self :)))
#python #logging #log #logger #formatter #log_formatter #space_padding #padding