def send_alert(message): m_log = Logger(logger_name='ssh_logger', filename = LogPath ) headers = {'Content-Type':'application/x-www-form-urlencoded'} msg = AlertContent + message payload = {'text': AlertTitle, 'desp': msg} try: resp = requests.post(ADDR, data=payload, headers=headers) except requests.exceptions.RequestException as e: m_log.info("send log failed: %s" %payload) m_log.info("send failed reason: %s" %e)
def test_info_logger(self): message = "test_logger is working?" # make sure logs directory is created if not os.path.isdir("logs"): os.system("mkdir logs") print("directory created") log = Logger() log.info(message) with open(log.filename, "r") as log_file: log_lines = log_file.readlines() # for this test to be reused, remove the file os.system("rm " + log.filename) # get last line last_log_line = log_lines[-1].rstrip("\n") self.assertEqual(last_log_line, "[INFO] " + message)
from logger_class import Logger logger_object = Logger("/var/log/class_logger.log") logger_object.info("This is an info message")
def extract_data(): # load id data file = open("movie.txt", "r") id_list = file.readlines() for i in range(len(id_list)): id_list[i] = id_list[i].strip("\n") # setup logger = Logger("file").getLogger() with MyDb("localhost", "root", "", 3306) as db, DoubanAPI() as D: # create database "movie_info", comment this block if database already created try: create_db(db) except Exception as e: logger.error(e) raise Exception("Database creation failed!") # connect to "movie_info" db.connect_to_db("movie_info") # begin extraction and insertion for id in id_list: time.sleep(3) # simulate user behaviour # extraction try: D.search(id) logger.info("Search success for id: " + str(id)) except Exception as e: # error caused by ip ban logger.error(e) raise Exception("Ip banned!") try: info = d.info() except Exception as e: # error caused by ip ban logger.error(e) raise Exception("Ip banned!") # insert into entity 'movie' query = "INSERT INTO `movie` VALUES (%s, %s, %s, %s, %s)" param = (id, ",".join(info["上映日期"]), info["rating"], info["rating_count"], ",".join(info["rating_weight"])) try: db.execute(query, param) except Exception as e: # error caused by duplicate id in txt logger.error(e) logger.info("Failure in id: " + str(id)) continue # insert into relations query = "INSERT INTO `director_movie` VALUES (%s, %s)" for d in info["director_id"]: try: param = (d, id) db.execute(query, param) except Exception as e: logger.error(e) continue query = "INSERT INTO `actor_movie` VALUES (%s, %s)" for a in info["actor_id"]: try: param = (a, id) db.execute(query, param) except Exception as e: logger.error(e) continue query = "INSERT INTO `writer_movie` VALUES (%s, %s)" for w in info["screenwriter_id"]: try: param = (w, id) db.execute(query, param) except Exception as e: logger.error(e) continue query = "INSERT INTO `actor_movie` VALUES (%s, %s)" for a in info["actor_id"]: try: param = (a, id) db.execute(query, param) except Exception as e: logger.error(e) continue query = "INSERT INTO `genre_movie` VALUES (%s, %s)" for g in info["类型"]: try: param = (g, id) db.execute(query, param) except Exception as e: logger.error(e) continue query = "INSERT INTO `region_movie` VALUES (%s, %s)" for r in info["制片国家/地区"]: try: param = (r, id) db.execute(query, param) except Exception as e: logger.error(e) continue query = "INSERT INTO `language_movie` VALUES (%s, %s)" for l in info["语言"]: try: param = (l, id) db.execute(query, param) except Exception as e: logger.error(e) continue # if success logger.info("Success in id: " + str(id)) logger.info("Finish insertion") time.sleep(3000) # wait until insertion finish successfully filter_data(db)
#!/usr/bin/env python #-*- coding: utf-8 -*- from logger_class import Logger m_log = Logger(logger_name='watcher', filename = 'catche.log' ) m_log.info('test1') m_log.warn('test warn') m_log.debug('test debug')
# -*-coding:utf8-*- from logger_class import Logger logger_object = Logger("logfile/log1.log") logger_object.info('This is an info message')
from logger_class import Logger logger_object = Logger('resouces/info.log') logger_object.info('hello, this is a test')