This commit is contained in:
aozhiwei 2019-08-26 23:39:53 +08:00
parent 30c25277eb
commit d4586eabaa
2 changed files with 14 additions and 3 deletions

View File

@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
import os
import time
import datetime
import traceback
import _thread
import threading
import q7
class _LocalLogNode:
def __init__(self, msg):
self.msg = msg
@ -21,6 +24,7 @@ class _UdpLog:
return cls._instance
def __init__(self):
self._logDir = ''
self._topNode = None
self._botNode = None
self._workNode = None
@ -29,6 +33,11 @@ class _UdpLog:
def init(self):
_thread.start_new_thread(self._saveToFile, ())
def setLogDirAndCreate(self, logdir):
if not os.path.exists(logdir):
os.makedirs(logdir)
self._logDir = logdir
def unInit(self):
pass
@ -72,9 +81,9 @@ class _UdpLog:
finally:
self._msgLock.release()
if self._workNode:
filename = datetime.datetime.now().strftime('log%Y-%m-%d.log')
filename = datetime.datetime.now().strftime('log_' + str(os.getpid()) + '_%Y%m%d.log')
try:
with open(os.path.join(LOG_DIR, filename), 'a') as f:
with open(os.path.join(self._logDir, filename), 'a') as f:
while self._workNode:
nextNode = self._workNode.nextNode
f.write(self._workNode.msg + '\r\n')

View File

@ -8,4 +8,6 @@ sys.path.append('..')
import f7.udplog
f7.udplog.instance().init()
time.sleep(5)
f7.udplog.instance().setLogDirAndCreate('/data/logs/f7test/logs')
f7.udplog.instance().info('start')
time.sleep(15)