Adds logging capabilities to output log file to drivername.log
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
"""Driver for {{cookiecutter.driver_name}}"""
|
"""Driver for {{cookiecutter.driver_name}}"""
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
|
import sys
|
||||||
from device_base import deviceBase
|
from device_base import deviceBase
|
||||||
from Channel import Channel, read_tag, write_tag, BoolArrayChannels
|
from Channel import Channel, read_tag, write_tag, BoolArrayChannels
|
||||||
from Maps import {{cookiecutter.driver_name}}_map as maps
|
from Maps import {{cookiecutter.driver_name}}_map as maps
|
||||||
@@ -10,9 +11,28 @@ from random import randint
|
|||||||
from utilities import get_public_ip_address
|
from utilities import get_public_ip_address
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
import logging
|
||||||
|
|
||||||
_ = None
|
_ = None
|
||||||
|
|
||||||
|
# LOGGING SETUP
|
||||||
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
|
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
|
||||||
|
logFile = './{{cookiecutter.driver_name}}.log'
|
||||||
|
my_handler = RotatingFileHandler(logFile, mode='a', maxBytes=500*1024, backupCount=2, encoding=None, delay=0)
|
||||||
|
my_handler.setFormatter(log_formatter)
|
||||||
|
my_handler.setLevel(logging.INFO)
|
||||||
|
logger = logging.getLogger('{{cookiecutter.driver_name}}')
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
logger.addHandler(my_handler)
|
||||||
|
|
||||||
|
console_out = logging.StreamHandler(sys.stdout)
|
||||||
|
console_out.setFormatter(log_formatter)
|
||||||
|
logger.addHandler(console_out)
|
||||||
|
|
||||||
|
logger.info("{{cookiecutter.driver_name}} startup")
|
||||||
|
|
||||||
# GLOBAL VARIABLES
|
# GLOBAL VARIABLES
|
||||||
WATCHDOG_SEND_PERIOD = 3600 # Seconds, the longest amount of time before sending the watchdog status
|
WATCHDOG_SEND_PERIOD = 3600 # Seconds, the longest amount of time before sending the watchdog status
|
||||||
PLC_IP_ADDRESS = "192.168.1.10"
|
PLC_IP_ADDRESS = "192.168.1.10"
|
||||||
@@ -50,7 +70,7 @@ class start(threading.Thread, deviceBase):
|
|||||||
for i in range(0, wait_sec):
|
for i in range(0, wait_sec):
|
||||||
print("{{cookiecutter.driver_name}} driver will start in {} seconds".format(wait_sec - i))
|
print("{{cookiecutter.driver_name}} driver will start in {} seconds".format(wait_sec - i))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
print("BOOM! Starting {{cookiecutter.driver_name}} driver...")
|
logger.info("BOOM! Starting {{cookiecutter.driver_name}} driver...")
|
||||||
|
|
||||||
public_ip_address = get_public_ip_address()
|
public_ip_address = get_public_ip_address()
|
||||||
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, '{{cookiecutter.driver_name}}')
|
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, '{{cookiecutter.driver_name}}')
|
||||||
@@ -63,7 +83,7 @@ class start(threading.Thread, deviceBase):
|
|||||||
watchdog_check_after = 5000
|
watchdog_check_after = 5000
|
||||||
while True:
|
while True:
|
||||||
if self.forceSend:
|
if self.forceSend:
|
||||||
print "FORCE SEND: TRUE"
|
logger.warning("FORCE SEND: TRUE")
|
||||||
|
|
||||||
for c in CHANNELS:
|
for c in CHANNELS:
|
||||||
if c.read(self.forceSend):
|
if c.read(self.forceSend):
|
||||||
@@ -73,7 +93,7 @@ class start(threading.Thread, deviceBase):
|
|||||||
# print("{{cookiecutter.driver_name}} driver still alive...")
|
# print("{{cookiecutter.driver_name}} driver still alive...")
|
||||||
if self.forceSend:
|
if self.forceSend:
|
||||||
if send_loops > 2:
|
if send_loops > 2:
|
||||||
print("Turning off forceSend")
|
logger.warning("Turning off forceSend")
|
||||||
self.forceSend = False
|
self.forceSend = False
|
||||||
send_loops = 0
|
send_loops = 0
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user