1

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
alforddm
  • 13
  • 3
  • Your `basicConfig` should be out of the while loop I think. Until basic config is in place, logging is not configured right? Often, people configure logging as the first step in their main module... – urban Oct 14 '19 at 16:29

1 Answers1

3

You are correct, calls to basicConfig() don't work in this scenario. Instead, you have to create a new handler every loop and swap it in for the old one.

import logging

# create a formatter
formatter = logging.Formatter('%(asctime)s %(message)s')

# get a logger
logger = logging.getLogger()

for i in range(1, 5):
    # remove any old handlers
    for old_handler in logger.handlers:
        logger.removeHandler(old_handler)

    # create a new handler
    new_filename = 'log' + str(i)
    file_handler = logging.FileHandler(filename=new_filename, mode='a')
    file_handler.setFormatter(formatter)

    # add the handler and use it
    logger.addHandler(file_handler)
    logger.warning('some message')

Code based on this answer.

On another note, using logging instead of L would be more readable, as would sticking to other PEP8 naming conventions.

FiddleStix
  • 3,016
  • 20
  • 21