| 1 |  |  | # Licensed to the StackStorm, Inc ('StackStorm') under one or more | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # contributor license agreements.  See the NOTICE file distributed with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # this work for additional information regarding copyright ownership. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # The ASF licenses this file to You under the Apache License, Version 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # (the "License"); you may not use this file except in compliance with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # the License.  You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | #     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | # Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | from __future__ import absolute_import | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | import logging.config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | import logging.handlers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | import traceback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | from functools import wraps | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | import six | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | from oslo_config import cfg | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | from st2common.logging.filters import ExclusionFilter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | # Those are here for backward compatibility reasons | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | from st2common.logging.handlers import FormatNamedFileHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | from st2common.logging.handlers import ConfigurableSyslogHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | from st2common.util.misc import prefix_dict_keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | __all__ = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     'getLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     'setup', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     'FormatNamedFileHandler', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     'ConfigurableSyslogHandler', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     'LoggingStream' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | logging.AUDIT = logging.CRITICAL + 10 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | logging.addLevelName(logging.AUDIT, 'AUDIT') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | LOGGER_KEYS = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     'debug', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     'info', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     'warning', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     'error', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     'critical', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     'exception', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     'log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     'audit' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | def decorate_log_method(func): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     @wraps(func) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     def func_wrapper(*args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         # Prefix extra keys with underscore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if 'extra' in kwargs: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             kwargs['extra'] = prefix_dict_keys(dictionary=kwargs['extra'], prefix='_') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return func(*args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         except TypeError as e: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             # In some version of Python 2.7, logger.exception doesn't take any kwargs so we need | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             # this hack :/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             # See: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             # - https://docs.python.org/release/2.7.3/library/logging.html#logging.Logger.exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             # - https://docs.python.org/release/2.7.7/library/logging.html#logging.Logger.exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             if 'got an unexpected keyword argument \'extra\'' in str(e): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 kwargs.pop('extra', None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 return func(*args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             raise e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     return func_wrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | def decorate_logger_methods(logger): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     Decorate all the logger methods so all the keys in the extra dictionary are | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     automatically prefixed with an underscore to avoid clashes with standard log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     record attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     for key in LOGGER_KEYS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         log_method = getattr(logger, key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         log_method = decorate_log_method(log_method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         setattr(logger, key, log_method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     return logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | def getLogger(name): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     logger_name = 'st2.{}'.format(name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     logger = logging.getLogger(logger_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     logger = decorate_logger_methods(logger=logger) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     return logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | class LoggingStream(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     def __init__(self, name, level=logging.ERROR): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         self._logger = getLogger(name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         self._level = level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     def write(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         self._logger._log(self._level, message, None) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | def _audit(logger, msg, *args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     if logger.isEnabledFor(logging.AUDIT): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         logger._log(logging.AUDIT, msg, args, **kwargs) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | logging.Logger.audit = _audit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | def _add_exclusion_filters(handlers): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     for h in handlers: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         h.addFilter(ExclusionFilter(cfg.CONF.log.excludes)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | def _redirect_stderr(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     # It is ok to redirect stderr as none of the st2 handlers write to stderr. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     if cfg.CONF.log.redirect_stderr: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         sys.stderr = LoggingStream('STDERR') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 132 |  |  | def setup(config_file, disable_existing_loggers=False): | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |     Configure logging from file. | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |     try: | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |         logging.config.fileConfig(config_file, | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |                                   defaults=None, | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |                                   disable_existing_loggers=disable_existing_loggers) | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |         handlers = logging.getLoggerClass().manager.root.handlers | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |         _add_exclusion_filters(handlers) | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |         _redirect_stderr() | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |     except Exception as exc: | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |         # revert stderr redirection since there is no logger in place. | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |         sys.stderr = sys.__stderr__ | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |         # No logger yet therefore write to stderr | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |         sys.stderr.write('ERROR: %s' % traceback.format_exc()) | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |         raise Exception(six.text_type(exc)) | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |  | 
            
                        
Prefixing a member variable
_is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: