0

I have one of my application (using python script) running on autostart using cron (debian based raspberry pi). I am writing logs to a file using the python logging module, based on the events occurring in the application. Now, I want to be able to see the logs for any errors in the program while it is running. For example, if one of my USB devices get damaged, then it will give an error that USB device not found, when the application starts up. Is there any way to see these errors in any of the inbuilt log files on the pi ? Or should we write error logs in the code. If so, how do I write logs to detect errors ?

2 Answers 2

2

There are two kind of errors when for example a USB device disconnected:

  1. System Errors
  2. Application Errors

System errors is logging in built-in log files (usually in /var/log/). In this case you could use rsyslog to categorize errors in files and watching them with another application.

Here you could find some useful rsyslog configuration: http://wiki.rsyslog.com/index.php/Configuration_Samples

I believe it's clear that in case of application error, you should log errors with application and watch them.

Sign up to request clarification or add additional context in comments.

Comments

0

OK, I re-wrote the answer based on your comments: Install PyUSB

Checking if the USB device is working:

def getUSBDeviceStatus(vendor, product):

    #idVendor 0xfffe, idProduct=0x0001
    dev = usb.core.find(idVendor=vendor, idProduct=product)
    if dev is None:
        raise ValueError("Device failed or is not connected!")
    else:
        return 1

Retrieving the logs based on their type:

def retrieveLogs(type):

    with open('/home/pi/log/log.log') as file:
        lines = sum(1 for _ in file)

    readerPosition = 0
    readingLine = file.readline() 

    while readerPosition >= lines:

        if readingLine.find(type):
            print readingLine
        readerPosition += 1

PS: I had the assumption you are logging errors with their respective criticality.

3 Comments

Hi, but this is not what I was looking for. FOr example, if the USB device connected to the ports is broken, when the code is loaded up, it would give me an error that USB device is not found in the python idle GUI. How do I write this to the logs ?
The comment makes it seem like you need to be notified of errors that your application isn't handling, or can't handle. You may need to get your application to actively scan syslog or logs from other apps.
thank u. I am using crontab to autostart the program, using the command @reboot sudo python /home/Desktop/Appllication.py Is it possible to write all the logs (errors and other stuff) by appending something to the above command, so that cron writes all errors/ any related events to a log ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.