def init_app(cls, app): Config.init_app(app) import logging from slack_log_handler import SlackLogHandler slackHandler = SlackLogHandler(Config.SLACK_LOG_URL) slackHandler.setLevel(logging.WARNING) app.logger.addHandler(slackHandler)
def Logger(logger_name, log_path, log_name): if not os.path.exists(log_path): try: os.makedirs(log_path) except Exception: # TODO: WTF pass # Get logger, set debug level logger = logging.getLogger(logger_name) logger.setLevel(logging.DEBUG) # Setup file handler log rotating by day qualified_log_name = log_path + '/' + log_name file_handler = TimedRotatingFileHandler(qualified_log_name, 'midnight', 1) file_handler.setFormatter( logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s %(filename)s %(funcName)s %(lineno)d')) file_handler.setLevel(logging.DEBUG) # Setup local handler to output to stderr local_handler = logging.StreamHandler(sys.stderr) local_handler.setLevel(logging.DEBUG) # Build/add slack handler if Config.SLACK_ERROR_LOGGING_ENABLED: slack_handler = SlackLogHandler(Config.SLACK_LOG_WEBHOOK_URL) slack_handler.setLevel(logging.ERROR) logger.addHandler(slack_handler) # Add handlers to logger logger.addHandler(file_handler) logger.addHandler(local_handler) return logger
def initialize_slack_logger(logger=None, *args, **kwargs): try: tag = os.environ.get('TAGS', 'worker') from slack_log_handler import SlackLogHandler slack_handler = SlackLogHandler( webhook_url=app.config["SLACK_WEBHOOK_URL"], channel=app.config["SLACK_CHANNEL"], format='{0} %(levelname)s - %(asctime)s - %(name)s - %(message)s'. format(tag)) slack_handler.setLevel(logging.ERROR) logger.addHandler(slack_handler) except urllib.error.HTTPError: LOGGER.info( "******************** Slack webhook is not working *********************" )
def getSlackHandler(): if SLACK_WEBHOOK_URL is None: return None from slack_log_handler import SlackLogHandler slack_handler = SlackLogHandler(SLACK_WEBHOOK_URL, username=SLACK_USERNAME) return slack_handler
def getMyLogger(ENV): logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s ["+ENV+"] %(message)s","%m/%d %H:%M:%S") handler = logging.StreamHandler() handler.setFormatter(formatter) logger.addHandler(handler) slack = SlackLogHandler( 'https://hooks.slack.com/services/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', username = '******' ) slack.setLevel(logging.INFO) slack.setFormatter(formatter) logger.addHandler(slack) return logger
def init_app(cls, app): Config.init_app(app) import logging import urllib # Log to docker logs app.logger.addHandler(logging.StreamHandler()) app.logger.setLevel(logging.INFO) # Error log to slack if cls.slack_response == 403: app.logger.info("****** Slack webhook is not correct ***********") else: if cls.SLACK_LOGGING: try: from slack_log_handler import SlackLogHandler slack_handler = SlackLogHandler( webhook_url=cls.SLACK_WEBHOOK_URL, channel=cls.SLACK_CHANNEL, format= '%(levelname)s - %(asctime)s - %(name)s - %(message)s') slack_handler.setLevel(logging.ERROR) app.logger.addHandler(slack_handler) except urllib.error.HTTPError: app.logger.info( "******************** Slack webhook is not working ******************" ) # Log to loki server if cls.LOKI_LOGGING: import logging_loki tag = os.environ.get('TAGS') try: handler = logging_loki.LokiHandler( url="{0}/loki/api/v1/push".format(cls.LOKI_URL), tags={"application": tag}, auth=(cls.LOKI_USERNAME, cls.LOKI_PASSWORD), version="1", ) app.logger.setLevel(logging.INFO) app.logger.addHandler(handler) except: pass
def __init__(self, *args, **kwargs): super(SlackLog, self).__init__(*args, **kwargs) self._config = { 'webhook_url': self.config.as_str('webhook_url', default=None), 'username': self.config.as_str('username', default='bot') } slack_handler = SlackLogHandler(self._config['webhook_url'], username=self._config['username'], emojis={ logging.INFO: ":white_circle:", logging.DEBUG: ":black_circle:", logging.ERROR: ":red_circle:" }) logFormat = "%(asctime)s %(levelname)s %(message)s" formatter = logging.Formatter(fmt=logFormat) slack_handler.setFormatter(formatter) self.logger = logging.getLogger("slacklog") self.logger.setLevel(logging.DEBUG) self.logger.addHandler(slack_handler) self.logger.setLevel(logging.DEBUG)
def initLogger(slackUrl=None): """Return logger.""" logger = logging.getLogger() logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s') # stream = logging.StreamHandler() stream.setLevel(logging.INFO) stream.setFormatter(formatter) logger.addHandler(stream) # if slackUrl is not None and len(slackUrl) > 0: slack = SlackLogHandler( slackUrl, username='******', emojis={ logging.INFO: ':grinning:', logging.WARNING: ':white_frowning_face:', logging.ERROR: ':persevere:', logging.CRITICAL: ':confounded:', }) slack.setLevel(logging.INFO) slack.setFormatter(formatter) logger.addHandler(slack) return logger
def get_slack_logger(name, hook): """Build a `logging.logger` instance with a special handler that will send a notification to cgstudiomap slack. :param str name: name of the logger. :param str hook: url of the hook to use to send the notification. :return: logging.logger instance. """ slack_handler = SlackLogHandler(hook, username='******') slack_handler.formatter = logging.Formatter( "%(asctime)s GMT - %(message)s", "%Y-%m-%d %H:%M") slack_handler.setLevel(logging.INFO) slack_handler.EMOJIS[logging.INFO] = ':jazz:' slack_handler.EMOJIS[logging.NOTSET] = ':jazz:' slack_handler.EMOJIS[logging.WARNING] = ':jazz:' slack_handler.EMOJIS[logging.ERROR] = ':jazz:' slack_logger = logging.getLogger(name) slack_logger.addHandler(slack_handler) slack_logger.info('Jazz Ready for action! (From: {0}@{1})'.format( getpass.getuser(), socket.gethostname())) return slack_logger
def getSlackHandler(level, formatter): from slack_log_handler import SlackLogHandler if level is None: return None slackHandler = SlackLogHandler(SLACK_WEBHOOK_URL, username=SLACK_USERNAME) slackHandler.setLevel(level) slackHandler.setFormatter(formatter) return slackHandler
def get_module_logger(name=None): global loggers if 'loggers' not in globals(): loggers = {} if name is None: name = __name__ if loggers.get(name): return loggers.get(name) # logger の基本設定 logger = getLogger(name) logger.setLevel(LOG_LEVEL_BASE) formatter = Formatter("%(asctime)s :%(filename)-10s %(lineno)-4d:%(levelname)-8s:%(funcName)-22s:%(message)s") # 標準出力 Handler sh = StreamHandler() logger.addHandler(sh) sh.setLevel(LOG_LEVEL_BASE) sh.setFormatter(formatter) # ファイル出力 Handler if LOG_FILE_DIR is not None: fh = FileHandler(LOG_FILE_DIR) fh.setLevel(LOG_LEVEL_FILE) logger.addHandler(fh) fh.setFormatter(formatter) # slack への通知 if SLACK_WEBHOOK_URL is not None: slack = SlackLogHandler( SLACK_WEBHOOK_URL, username = HOSTNAME, emojis = { logging.INFO: ':cubimal_chick:', logging.WARNING: ':cloud:', logging.ERROR: ':japanese_goblin:', logging.CRITICAL: ':exploding_head:', } ) slack.setLevel(LOG_LEVEL_SLACK) slack.setFormatter(formatter) logger.addHandler(slack) logger.propagate = False loggers[name] = logger ln = lambda nolevel: logging.getLevelName(nolevel) # logger.info(f''' # 【 LOG CONFIG 】 # - LOG_LEVEL_BASE: {ln(LOG_LEVEL_BASE)}, # - LOG_FILE_DIR: {LOG_FILE_DIR}, # - LOG_LEVEL_FILE: {ln(LOG_LEVEL_FILE)}, # - SLACK_WEBHOOK_URL: {SLACK_WEBHOOK_URL}, # - LOG_LEVEL_SLACK: {ln(LOG_LEVEL_SLACK)}, # ''') return logger
def make_root_logger(console_loglevel: int = logging.DEBUG, file_loglevel: int = STOP_LOG, slack_loglevel: int = STOP_LOG, log_file_path: str = None) -> logging.Logger: """Generate the root logger, including Slack log handler. Args: console_loglevel: the logging level of console handler. file_loglevel: the logging level of file log handler. slack_loglevel: the logging level of slack log handler. log_file_path: the path of log file. Returns: the root logger """ root_logger = logging.getLogger() root_logger.setLevel(logging.DEBUG) formatter = logging.Formatter( fmt= "[%(levelname)s] %(asctime)s %(module)s::%(funcName)s, line %(lineno)d >> %(message)s" ) if console_loglevel < STOP_LOG: console_handler = logging.StreamHandler() console_handler.setFormatter(formatter) console_handler.setLevel(console_loglevel) root_logger.addHandler(console_handler) if log_file_path is not None and file_loglevel < STOP_LOG: file_handler = logging.FileHandler(log_file_path) file_handler.setFormatter(formatter) file_handler.setLevel(file_loglevel) root_logger.addHandler(file_handler) if slack_loglevel < STOP_LOG: env_val_name = "WEBHOOK_URL" try: webhook_url = os.environ[env_val_name] except KeyError as e: root_logger.error( "Environment variable \"{0}\" is not defined, so root logger cannot log to Slack." .format(env_val_name) + "Do \"export {0}=(WebHook URL)\" in the terminal or".format( env_val_name) + "edit environment variables in Edit Configurations, PyCharm". format(env_val_name)) else: slack_handler = SlackLogHandler(webhook_url) slack_handler.setFormatter(formatter) slack_handler.setLevel(slack_loglevel) root_logger.addHandler(slack_handler) return root_logger
def init_slack(): if not os.path.exists(HOME + '/.slack_hook'): return wh = json.loads(open(HOME + '/.slack_hook').read()) formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s') slack = SlackLogHandler(wh['webhook'], username='******', emojis={ logging.INFO: ':grinning:', logging.WARNING: ':white_frowning_face:', logging.ERROR: ':persevere:', logging.CRITICAL: ':confounded:', }) slack.setLevel(logging.INFO) slack.setFormatter(formatter) logger.addHandler(slack)
def getMyLogger(ENV): logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s [" + ENV + "] %(message)s", "%m/%d %H:%M:%S") handler = logging.StreamHandler() handler.setFormatter(formatter) logger.addHandler(handler) slack = SlackLogHandler( 'https://hooks.slack.com/services/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', username='******') slack.setLevel(logging.INFO) slack.setFormatter(formatter) logger.addHandler(slack) return logger
if not success: if not waiting: logger.info("No experiments to run, waiting for more") waiting = True time.sleep(5) else: waiting = False if __name__ == "__main__": # Configure Logger logger = logging.getLogger("DaemonLog") logger.setLevel(logging.INFO) formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler = logging.FileHandler("/home/ubuntu/experiments/runner.log") handler.setFormatter(formatter) logger.addHandler(handler) slack_handler = SlackLogHandler( "https://hooks.slack.com/services/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" ) slack_handler.setLevel(logging.INFO) logger.addHandler(slack_handler) # Configure app to run as daemon exp_runner = ExperimentRunner() daemon_runner = runner.DaemonRunner(exp_runner) daemon_runner.daemon_context.files_preserve = [handler.stream] daemon_runner.do_action()
logger.addHandler(stream_handler) # ファイル出力(debug) dfile_handler = logging.FileHandler('collect/log/debug.log', 'a') dfile_handler.setLevel(logging.DEBUG) dfile_handler.setFormatter(format) logger.addHandler(dfile_handler) # ファイル出力(error) efile_handler = logging.FileHandler('collect/log/error.log', 'a') efile_handler.setLevel(logging.ERROR) efile_handler.setFormatter(format) logger.addHandler(efile_handler) # slack slack_handler = SlackLogHandler(webhook_url=alert_url, emojis={ logging.INFO: ':grinning:', logging.WARNING: ':white_frowning_face:', logging.ERROR: ':persevere:', logging.CRITICAL: ':confounded:', }) slack_handler.setLevel(logging.ERROR) slack_handler.setFormatter(format) logger.addHandler(slack_handler) class SimpleListener(SubscribeListener): def __init__(self, conn): super().__init__() self.conn = conn def create_query(self): query = 'INSERT INTO crypto.candlestick (' \
import os import logging from slack_log_handler import SlackLogHandler WEBHOOK_URL = os.getenv('SLACK_URL') slack_handler = SlackLogHandler(WEBHOOK_URL) slack_handler.setLevel(logging.WARNING) logger = logging.getLogger(__name__) logger.addHandler(slack_handler) logger.error('Oh my god, an error occurred!')
def run(self): waiting = False while True: success = self.do_next_experiment() if not success: if not waiting: logger.info("No experiments to run, waiting for more") waiting = True time.sleep(5) else: waiting = False if __name__ == "__main__": # Configure Logger logger = logging.getLogger("DaemonLog") logger.setLevel(logging.INFO) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler = logging.FileHandler("/home/ubuntu/experiments/runner.log") handler.setFormatter(formatter) logger.addHandler(handler) slack_handler = SlackLogHandler("https://hooks.slack.com/services/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") slack_handler.setLevel(logging.INFO) logger.addHandler(slack_handler) # Configure app to run as daemon exp_runner = ExperimentRunner() daemon_runner = runner.DaemonRunner(exp_runner) daemon_runner.daemon_context.files_preserve=[handler.stream] daemon_runner.do_action()
# # 標準出力 Handler # stream = logging.StreamHandler() stream.setLevel(logging.DEBUG) stream.setFormatter(formatter) # # Slack Handler # slack = SlackLogHandler(os.getenv( 'SLACK_WEB_HOOK', "https://hooks.slack.com/services/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" ), username='******', emojis={ logging.INFO: ':grinning:', logging.WARNING: ':white_frowning_face:', logging.ERROR: ':persevere:', logging.CRITICAL: ':confounded:', }) slack.setLevel(logging.INFO) slack.setFormatter(formatter) # # logger に Handler を追加 # logger.addHandler(stream) logger.addHandler(slack) if __name__ == '__main__':
def get_slack_handler(): from slack_log_handler import SlackLogHandler slack_handler = SlackLogHandler(SLACK_WEBHOOK_URL, username=SLACK_USERNAME) return slack_handler