|
| 1 | +""" |
| 2 | +Stackify Python API |
| 3 | +""" |
| 4 | + |
| 5 | +__version__ = '0.0.1' |
| 6 | + |
| 7 | + |
| 8 | +API_URL = 'https://api.stackify.com' |
| 9 | + |
| 10 | +READ_TIMEOUT = 5000 |
| 11 | + |
| 12 | +MAX_BATCH = 100 |
| 13 | + |
| 14 | +QUEUE_SIZE = 1000 |
| 15 | + |
| 16 | +import logging |
| 17 | +import inspect |
| 18 | +import atexit |
| 19 | + |
| 20 | +DEFAULT_LEVEL = logging.ERROR |
| 21 | + |
| 22 | +LOGGING_LEVELS = { |
| 23 | + logging.CRITICAL: 'CRITICAL', |
| 24 | + logging.ERROR: 'ERROR', |
| 25 | + logging.WARNING: 'WARNING', |
| 26 | + logging.INFO: 'INFO', |
| 27 | + logging.DEBUG: 'DEBUG', |
| 28 | + logging.NOTSET: 'NOTSET' |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +class NullHandler(logging.Handler): |
| 33 | + def emit(self, record): |
| 34 | + pass |
| 35 | + |
| 36 | +logging.getLogger(__name__).addHandler(NullHandler()) |
| 37 | + |
| 38 | + |
| 39 | +from stackify.application import ApiConfiguration |
| 40 | +from stackify.http import HTTPClient |
| 41 | + |
| 42 | +from stackify.handler import StackifyHandler |
| 43 | + |
| 44 | + |
| 45 | +def getLogger(name=None, auto_shutdown=True, basic_config=True, **kwargs): |
| 46 | + '''Get a logger and attach a StackifyHandler if needed. |
| 47 | +
|
| 48 | + You can pass this function keyword arguments for Stackify configuration. |
| 49 | + If they are omitted you can specify them through environment variables: |
| 50 | + * STACKIFY_API_KEY |
| 51 | + * STACKIFY_APPLICATION |
| 52 | + * STACKIFY_ENVIRONMENT |
| 53 | + * STACKIFY_API_URL |
| 54 | +
|
| 55 | + Args: |
| 56 | + name: The name of the logger (or None to automatically make one) |
| 57 | + auto_shutdown: Register an atexit hook to shut down logging |
| 58 | + basic_config: Set up with logging.basicConfig() for regular logging |
| 59 | +
|
| 60 | + Optional Args: |
| 61 | + api_key: Your Stackify API key |
| 62 | + application: The name of your Stackify application |
| 63 | + environment: The Stackfiy environment to log to |
| 64 | + api_url: An optional API url if required |
| 65 | +
|
| 66 | + Returns: |
| 67 | + A logger instance with Stackify handler and listener attached. |
| 68 | + ''' |
| 69 | + if basic_config: |
| 70 | + logging.basicConfig() |
| 71 | + |
| 72 | + if not name: |
| 73 | + name = getCallerName(2) |
| 74 | + |
| 75 | + logger = logging.getLogger(name) |
| 76 | + |
| 77 | + if not [isinstance(x, StackifyHandler) for x in logger.handlers]: |
| 78 | + internal_logger = logging.getLogger(__name__) |
| 79 | + internal_logger.debug('Creating handler for logger %s', name) |
| 80 | + handler = StackifyHandler(**kwargs) |
| 81 | + logger.addHandler(handler) |
| 82 | + |
| 83 | + if auto_shutdown: |
| 84 | + internal_logger.debug('Registering atexit callback') |
| 85 | + atexit.register(stopLogging, logger) |
| 86 | + |
| 87 | + if logger.getEffectiveLevel() == logging.NOTSET: |
| 88 | + logger.setLevel(DEFAULT_LEVEL) |
| 89 | + |
| 90 | + handler.listener.start() |
| 91 | + |
| 92 | + return logger |
| 93 | + |
| 94 | + |
| 95 | +def stopLogging(logger): |
| 96 | + '''Stop logging on the Stackify handler. |
| 97 | +
|
| 98 | + Shut down the StackifyHandler on a given logger. This will block |
| 99 | + and wait for the queue to finish uploading. |
| 100 | + ''' |
| 101 | + internal_logger = logging.getLogger(__name__) |
| 102 | + internal_logger.debug('Shutting down all handlers') |
| 103 | + for handler in getHandlers(logger): |
| 104 | + handler.listener.stop() |
| 105 | + |
| 106 | + |
| 107 | +def getCallerName(levels=1): |
| 108 | + '''Gets the name of the module calling this function''' |
| 109 | + try: |
| 110 | + frame = inspect.stack()[levels] |
| 111 | + module = inspect.getmodule(frame[0]) |
| 112 | + name = module.__name__ |
| 113 | + except IndexError: |
| 114 | + name = 'stackify-python-unknown' |
| 115 | + return name |
| 116 | + |
| 117 | + |
| 118 | +def getHandlers(logger): |
| 119 | + '''Return the StackifyHandlers on a given logger''' |
| 120 | + return [x for x in logger.handlers if isinstance(x, StackifyHandler)] |
0 commit comments