-
-
Save bcho/6863430 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
from logging.handlers import SMTPHandler as _SMTPHandler | |
class SMTPHandler(_SMTPHandler): | |
def __init__(self, *args, **kwargs): | |
super(SMTPHandler, self).__init__(*args, **kwargs) | |
self._timeout = 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Logging: | |
formatters: | |
brief: | |
format: "%(levelname)s <PID %(process)d:%(processName)s> %(name)s %(message)s" | |
filters: [] | |
handlers: | |
console: | |
class: logging.StreamHandler | |
formatter: brief | |
email: | |
class: handlers.SMTPHandler | |
mailhost: !!python/tuple [smtp.gmail.com, 587] | |
fromaddr: [email protected] | |
toaddrs: [[email protected]] | |
subject: 'hello, this is from logging' | |
credentials: !!python/tuple [[email protected], yourpwd] | |
secure: !!python/tuple [] # force to start tls | |
file: | |
class: logging.handlers.RotatingFileHandler | |
filename: 'app.log' | |
loggers: | |
app: | |
propagate: true | |
level: INFO | |
handlers: [console] | |
app.notice: | |
propagate: false | |
level: WARNING | |
handlers: [console, file] | |
disable_existing_loggers: true | |
incremental: false | |
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
from time import sleep | |
import yaml | |
import logging | |
basic_logger = logging.getLogger('app') | |
notice_logger = logging.getLogger('app.notice') | |
def cal(): | |
a, b = 1, 1 | |
while True: | |
yield a + b | |
a, b = b, a + b | |
def is_noticable(x): | |
return x > 1000 and x % 5 == 0 | |
def main(): | |
for x in cal(): | |
basic_logger.info('now: %d' % x) | |
if is_noticable(x): | |
notice_logger.warn('look: %d' % x) | |
sleep(1) | |
if __name__ == '__main__': | |
import logging.config | |
config = yaml.load(open('logging.yaml', 'r')) | |
print config | |
logging.config.dictConfig(config['Logging']) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment