Allen Li | 20dd90f | 2017-07-17 12:27:10 -0700 | [diff] [blame^] | 1 | # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """lucifer scripts shared logging related functions.""" |
| 6 | |
| 7 | from __future__ import absolute_import |
| 8 | from __future__ import division |
| 9 | from __future__ import print_function |
| 10 | |
| 11 | import logging |
| 12 | import logging.config |
| 13 | |
| 14 | |
| 15 | def add_logging_options(parser): |
| 16 | """Add logging configuration options to argument parser. |
| 17 | |
| 18 | @param parser: ArgumentParser instance. |
| 19 | """ |
| 20 | # Unused for the moment, but will be useful when we need to add |
| 21 | # logging options. |
| 22 | del parser |
| 23 | |
| 24 | |
| 25 | def configure_logging_with_args(parser, args): |
| 26 | """Convenience function for calling configure_logging(). |
| 27 | |
| 28 | @param parser: ArgumentParser instance. |
| 29 | @param args: Return value from ArgumentParser.parse_args(). |
| 30 | """ |
| 31 | # Unused for the moment, but will be useful when we need to add |
| 32 | # logging options. |
| 33 | del args |
| 34 | configure_logging(name=parser.prog) |
| 35 | |
| 36 | |
| 37 | def configure_logging(name): |
| 38 | """Configure logging globally. |
| 39 | |
| 40 | @param name: Name to prepend to log messages. |
| 41 | This should be the name of the program. |
| 42 | """ |
| 43 | logging.config.dictConfig({ |
| 44 | 'version': 1, |
| 45 | 'formatters': { |
| 46 | 'stderr': { |
| 47 | 'format': ('{name}: ' |
| 48 | '%(asctime)s:%(levelname)s' |
| 49 | ':%(module)s:%(funcName)s:%(lineno)d' |
| 50 | ':%(message)s' |
| 51 | .format(name=name)), |
| 52 | }, |
| 53 | }, |
| 54 | 'handlers': { |
| 55 | 'stderr': { |
| 56 | 'class': 'logging.StreamHandler', |
| 57 | 'formatter': 'stderr' , |
| 58 | } |
| 59 | }, |
| 60 | 'root': { |
| 61 | 'level': 'DEBUG', |
| 62 | 'handlers': ['stderr'], |
| 63 | }, |
| 64 | 'disable_existing_loggers': False, |
| 65 | }) |