Fail to create log file from

  • I have set up a logging class in my, 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 
        def clear(self):
            with open(self.file_name,"w")#remove closed file

    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")
    # Setting up the UART to dump the output to the console
    uart = UART(0, 115200)
    logger.write("UART enabled and console dumped")
    # Disable bluetooth
    bluetooth = Bluetooth()
    logger.write("Bluetooth initialized")
    logger.write("Bluetooth disabled")
    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")

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

  • @beelzmon
    I thinkk there is a problem with the line
    from import Logger
    in That one causes to be compiled and all statements at top level to be executed. So you get the log messages eventually before the 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

    Since both and 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 runs after so there would be no logger available for

Log in to reply

Pycom on Twitter