This is a daemon that works as a electric kiln controller on a raspberry pi. I only want to start logging if it is actually set to run, not if it's just waiting to run. That way, I can log each individual kiln runs by RunID rather than just having a generically named log file.
If I set up the log code outside the while and if statements, it works fine. If I set it inside the while and if statements, the log file is never created. I used the exact same code both places to check functionality.
This works when placed at the start of the file just down from the import statements.
#--- Set up logging ---
LogFile = time.strftime(AppDir + '/log/%H_%M_%d_%m_%Y_pilnfired.log')
#LogFile = time.strftime(AppDir + '/log/%(asctime)s.log')
L.basicConfig(filename=LogFile,
#comment to disable
level=L.DEBUG,
format='%(asctime)s %(message)s'
)
If does not work when placed after this...
while 1:
#ReadTmp = TempRise
ReadTmp = Sensor0.read_temp_c()
ReadITmp = Sensor0.read_internal_temp_c()
# roomTmp = Sensor1.read_temp_c()
# roomITmp = Sensor1.read_internal_temp_c()
while math.isnan(ReadTmp):
#ReadTmp = TempRise
ReadTmp = Sensor0.read_temp_c()
print (' "kilntemp": "' + str(int(ReadTmp)) + '",\n')
L.debug("Write status information to status file %s:" % StatFile)
sfile = open(StatFile, "w+")
sfile.write('{\n' +
' "proc_update_utime": "' + str(int(time.time())) + '",\n'
+ ' "readtemp": "' + str(int(ReadTmp)) + '",\n'
+ ' "run_profile": "none",\n'
+ ' "run_segment": "n/a",\n'
+ ' "ramptemp": "n/a",\n'
+ ' "status": "n/a",\n'
+ ' "targettemp": "n/a"\n'
+ '}\n'
)
sfile.close()
# --- Check for 'Running' firing profile ---
sql = "SELECT * FROM profiles WHERE state=?;"
p = ('Running',)
SQLCur.execute(sql, p)
Data = SQLCur.fetchall()
#--- if Running profile found, then set up to fire, woowo! --
if len(Data) > 0:
RunID = Data[0]['run_id']
Kp = float(Data[0]['p_param'])
Ki = float(Data[0]['i_param'])
Kd = float(Data[0]['d_param'])
L.info("RunID: %d" % (RunID))
StTime = time.strftime('%Y-%m-%d %H:%M:%S')
sql = "UPDATE profiles SET start_time=? WHERE run_id=?;"
p = (StTime, RunID)
try:
SQLCur.execute(sql, p)
SQLConn.commit()
except:
SQLConn.rollback()
LogFile = time.strftime(AppDir + '/log/%H_%M_%d_%m_ %Y_pilnfired.log')
#LogFile = time.strftime(AppDir + '/log/%(asctime)s.log')
L.basicConfig(filename=LogFile,
#comment to disable
level=L.DEBUG,
format='%(asctime)s %(message)s'
)
Do more stuff...
There are no error messages the log is just never created. I know it's actually running through this part of code because I placed a print function right after and it printed.
I can create an alternative file and log all the messages to it but would be nice to use the logging function.