| 1 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from functools import wraps | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from ._compat import PY3, string_types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | if PY3: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     _wrapped_method_names = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         name.lower() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         for name in logging._nameToLevel.keys() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     _wrapped_method_names = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         name.lower() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         for name in logging._levelNames.keys() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         if isinstance(name, string_types) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | _wrapped_method_names.update({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     'exception', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class LoggerProxy(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     Proxy for `logging.Logger` and classes inherited from it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     Automatically injects prefixes to messages which are send to log. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     This reduces overhead of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         >>> logger.log_level(injector.mark("mesage")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     just to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         >>> logger.log_level("message") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     def __init__(self, logger, injector): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         self._original = logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         self._injector = injector | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |     def __getattr__(self, name): | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         Get attribute of original logger or wrapped version of methods used | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         for logging. | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         We do not wrap calls to `debug`, `info`, etc. directly. This is because | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         those levels are default, but their list may be extended, for example, | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         like in case of `python-verboselogs` library. | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         result = getattr(self._original, name) | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         if name in _wrapped_method_names: | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |             @wraps(result) | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             def wrapper(message, *args, **kwargs): | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |                 return result(self._injector.mark(message), *args, **kwargs) | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             # Cache wrapper, so it won't be constructed again for future calls. | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |             setattr(self, name, wrapper) | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             return wrapper | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         return result | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  |