Skip to content

Instantly share code, notes, and snippets.

@mikefromit
Last active September 5, 2016 23:09
Show Gist options
  • Save mikefromit/e5f1c6485b0752e103a5d5496d23f1fd to your computer and use it in GitHub Desktop.
Save mikefromit/e5f1c6485b0752e103a5d5496d23f1fd to your computer and use it in GitHub Desktop.
Google style logging

logging.py

def setup_logging():
    """Setup Google-style logging

    From: https://github.com/twitter/commons/blob/master/src/python/twitter/common/log/formatters/glog.py
    """
    fmt = '%(levelletter)s%(asctime)s.%(msecs).03d %(process)d %(filename)s:%(lineno)d] %(message)s'
    datefmt= '%m%d %H:%M:%S'
    formatter = CustomFormatter(fmt, datefmt)

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

    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
    root.addHandler(console_handler)
    
    return


def log_loading_messages(app, host, port):
    log = logging.getLogger(__name__)
    log.info('* Flask version: {}'.format(flask.__version__))
    log.info('* DEBUG: {}'.format(app.config['DEBUG']))
    
    return

wsgi.py

app = create_app()

if __name__ == '__main__':
    host = '127.0.0.1'
    port = 5000
    setup_logging()
    log_loading_messages(app, host, port)
    app.run(host, port)

OUTPUT

I0728 11:51:25.131 27617 logging.py:39] * Flask version: 0.11.1
I0728 11:51:25.131 27617 logging.py:40] * DEBUG: True
I0728 11:51:25.153 27617 _internal.py:87]  * Running on http://127.0.0.1:5000/
I0728 11:51:25.154 27617 _internal.py:87]  * Restarting with stat
W0728 11:51:25.462 27618 _internal.py:87]  * Debugger is active!
I0728 11:51:25.471 27618 _internal.py:87]  * Debugger pin code: 867-67-3982
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment