forked from togolife/baidu-wenku-download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
36 lines (29 loc) · 889 Bytes
/
log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
class Log(object):
def __init__(self, dirName, fileName):
dirName = './' + dirName
self.fileName = dirName + '/' + fileName
if not os.path.exists(dirName):
os.mkdir(dirName)
def __log(self, level, message):
curr_time = int(time.time())
time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(curr_time))
message = time_str + " [" + level + "] " + message + "\n"
fp = open(self.fileName, 'a')
fp.write(message)
fp.close()
def info(self, message):
self.__log("INFO", message)
def error(self, message):
self.__log("ERROR", message)
def warn(self, message):
self.__log("WARNING", message)
# test
if __name__ == '__main__':
log = Log('log', 'wenku-download.log')
log.info("测试开始")
log.error("发生错误了")
log.warn("龟拳警告")