Fail to create log file from boot.py



  • I have set up a logging class in my main.py, being:

    class Logger:
        file_name = None
        def __init__(self,file_name):
            self.file_name = file_name
        def write(self,msg):
            with open(self.file_name,"a") as file:#write a message to log 
                file.write(msg)
                file.write('\n')
        def clear(self):
            with open(self.file_name,"w")#remove closed file
                pass
    

    In my boot file and my main file i call on it to create logs, however it does not create any of the logs it should for the boot file -- only for the main.
    Below is the boot:

    from machine import UART
    from network import LoRa
    from network import WLAN,Bluetooth
    from main import Logger
    import binascii
    import os
    import pycom
    logger = Logger("SessionLog.txt")
    logger.write("Booting")
    pycom.heartbeat(False)
    # Setting up the UART to dump the output to the console
    uart = UART(0, 115200)
    os.dupterm(uart)
    logger.write("UART enabled and console dumped")
    # Disable bluetooth
    bluetooth = Bluetooth()
    logger.write("Bluetooth initialized")
    bluetooth.deinit()
    logger.write("Bluetooth disabled")
    pycom.wifi_on_boot(False)
    logger.write("Wifi on boot turned off")
    lora = LoRa(mode=LoRa.LORAWAN, public=1, adr=0, tx_retries=0) # Getting the LoRa MAC
    logger.write("Lora initialized for grabbing MAC address")
    print("LORA MAC")
    logger.write("LORA MAC")
    print(binascii.hexlify(lora.mac()))
    logger.write(binascii.hexlify(lora.mac()))
    

    The same sort of routine logs are written in the main.py, and these are the only lines reflected in the SessionLog.txt file. So the issue is that the boot.py specifically does not write to the file. No error is raised.



  • @beelzmon
    I thinkk there is a problem with the line
    from main.py import Logger
    in boot.py. That one causes main.py to be compiled and all statements at top level to be executed. So you get the main.py log messages eventually before the boot.py log messages in the log file.
    You could try to either:

    • put the class logger into a separate file, or
    • put the class logger into boot.py.

    Since both boot.py and main.py share the same name space, the class logger would be available to both. I prefer the separate file for the logger class.



  • My understanding is main.py runs after boot.py so there would be no logger available for boot.py


Log in to reply
 

Pycom on Twitter