<s> from __future__ import print_function <EOL> import json <EOL> import traceback <EOL> import requests <EOL> from enum import IntEnum , unique <EOL> __author__ = '<STR_LIT>' <EOL> @ unique <EOL> class LogLv ( IntEnum ) : <EOL> DEBUG = <NUM_LIT:10> <EOL> INFO = <NUM_LIT:20> <EOL> WARN = <NUM_LIT:30> <EOL> ERROR = <NUM_LIT> <EOL> LOG_LEVELS = list ( map ( int , LogLv ) ) <EOL> class SlackLogger : <EOL> def __init__ ( self , web_hook_url , channel = None , username = '<STR_LIT>' ) : <EOL> self . web_hook_url = web_hook_url <EOL> self . username = username <EOL> self . log_level = LogLv . INFO <EOL> if channel is None : <EOL> self . channel = None <EOL> elif channel . startswith ( '<STR_LIT:#>' ) or channel . startswith ( '<STR_LIT:@>' ) : <EOL> self . channel = channel <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def set_log_level ( self , lv ) : <EOL> if lv in LOG_LEVELS : <EOL> self . log_level = lv <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def __build_payload ( self , message , title , title_link , color , fields ) : <EOL> __attachments = [ { <EOL> '<STR_LIT:title>' : title , <EOL> '<STR_LIT>' : title_link , <EOL> '<STR_LIT>' : color , <EOL> '<STR_LIT:text>' : message , <EOL> '<STR_LIT>' : fields , <EOL> '<STR_LIT>' : [ '<STR_LIT:text>' , '<STR_LIT>' , '<STR_LIT:title>' ] <EOL> } ] <EOL> payload = { <EOL> '<STR_LIT>' : self . channel , <EOL> '<STR_LIT:username>' : self . username , <EOL> '<STR_LIT>' : __attachments , <EOL> } <EOL> return payload <EOL> def __send_notification ( self , message , title , title_link = '<STR_LIT>' , color = '<STR_LIT>' , <EOL> fields = '<STR_LIT>' , log_level = LogLv . INFO ) : <EOL> """<STR_LIT>""" <EOL> if log_level < self . log_level : <EOL> return None <EOL> payload = self . __build_payload ( message , title , title_link , color , fields ) <EOL> try : <EOL> response = requests . post ( self . web_hook_url , <EOL> data = json . dumps ( payload ) , timeout = <NUM_LIT:3> , allow_redirects = False ) <EOL> except Exception : <EOL> raise Exception ( traceback . format_exc ( ) ) <EOL> else : <EOL> if response . status_code == <NUM_LIT:200> : <EOL> return response <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def debug ( self , message , title = '<STR_LIT>' , title_link = '<STR_LIT>' , fields = '<STR_LIT>' ) : <EOL> return self . __send_notification ( message = message , <EOL> title = title , <EOL> title_link = title_link , <EOL> color = '<STR_LIT>' , <EOL> fields = fields , <EOL> log_level = LogLv . DEBUG ) <EOL> def info ( self , message , title = '<STR_LIT>' , title_link = '<STR_LIT>' , fields = '<STR_LIT>' ) : <EOL> return self . __send_notification ( message = message , <EOL> title = title , <EOL> title_link = title_link , <EOL> color = '<STR_LIT>' , <EOL> fields = fields , <EOL> log_level = LogLv . INFO ) <EOL> def warn ( self , message , title = '<STR_LIT>' , title_link = '<STR_LIT>' , fields = '<STR_LIT>' ) : <EOL> return self . __send_notification ( message = message , <EOL> title = title , <EOL> title_link = title_link , <EOL> color = '<STR_LIT>' , <EOL> fields = fields , <EOL> log_level = LogLv . WARN ) <EOL> def error ( self , message , title = '<STR_LIT>' , title_link = '<STR_LIT>' , fields = '<STR_LIT>' ) : <EOL> return self . __send_notification ( message = message , <EOL> title = title , <EOL> title_link = title_link , <EOL> color = '<STR_LIT>' , <EOL> fields = fields , <EOL> log_level = LogLv . ERROR ) <EOL> def message ( self , message , title = '<STR_LIT>' , title_link = '<STR_LIT>' , color = '<STR_LIT>' , fields = '<STR_LIT>' , <EOL> log_level = LogLv . ERROR ) : <EOL> return self . __send_notification ( message = message , <EOL> title = title , <EOL> title_link = title_link , <EOL> color = color , <EOL> fields = fields , <EOL> log_level = log_level ) </s>
