Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Python services and logging

Tue Oct 28, 2014 2:20 pm

Hello all,
i've some python scripts that i run as linux daemons. Today i discovered that one of these (the one that collects data from mqtt topics and store to db) was not running since last week, so maybe i have to take a little more care on logging :)

Now, the question. What's the correct way to log services?
I mean, probably (i do not know since of my linux noobness) the output of my scripts is logged in some "standard" services output, but this would be also difficult to manage.
So, my idea was to write myself the "logging ability" of my scripts. Something like, different level of logging with different management (like: info is only stored to file, error is stored and mailed), log purging and so on.

But i think i'll not be the first to think about something like that, so maybe there is already a python class/module for this?
And since i'm writing (ok, trying to write :D) scripts that can work with python 2 AND python 3, i'd like also a class/modulo compatible with both :)

So, how do you manage logging? have you any suggestion for me?

As always, thanks a lot..

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python services and logging

Tue Oct 28, 2014 4:25 pm

Python has a logging module. https://docs.python.org/2/howto/logging ... c-tutorial.

Use this to write to a logfile and you can then review the output as necessary.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python services and logging

Tue Oct 28, 2014 5:21 pm

I use something like this:

Code: Select all

import logging

LOG_LEVEL = logging.INFO
#LOG_LEVEL = logging.DEBUG
LOG_FILE = "/var/log/mylog"
#LOG_FILE = "/dev/stdout"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
logging.basicConfig(filename=LOG_FILE, format=LOG_FORMAT, level=LOG_LEVEL)
....
logging.info("Informational message")
logging.debug("Helpful debugging info")
logging.error("Something bad happened")
That's pretty basic, but as far as I can tell, logging lets you configure different handlers (rotating files, SMTP, etc.) for different logging levels.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Python services and logging

Wed Oct 29, 2014 7:06 am

Thank you guys, that's EXACTLY what i was looking for!
I'll give it a look :)

ground_
Posts: 17
Joined: Tue Oct 14, 2014 6:13 am

Re: Python services and logging

Thu Oct 30, 2014 3:46 am

I wrote the following logging script for my script last week. It prints a status on the screen every 30 min (obviously you can adjust that), and it writes that info to a .txt file. (i adjusted it a little bit so it would be more obvious)

Code: Select all

 import threading
        tlog= threading.Timer(30,log)
        tlog.start()

        if logs == 1: #this will be one if i set the logging option to off
            tlog.cancel()

        else:
            tempfile=open( "/sys/class/thermal/thermal_zone0/temp","r" )#get cpu temperature
            cpu_temp=float(tempfile.read())//1000
            tempfile.close()

            print("\n") #print info on screen
            print("."*44)
            print(time.strftime("%m/%d/%Y")," - ",time.strftime("%H:%M:%S" ))
            print("RPI CPU temperature: %s °C" %(cpu_temp))
            if gpio11 =="true":
                print("settings for the gpio11: %s,%s,%s" %(lgpio11_1,gpio11_2,gpio11_3))
            if gpio13 =="true":
                print("settings for the gpio13: %s,%s,%s" %(lgpio13_1,gpio13_2,gpio13_3))
            if gpio15 =="true":
                print("settings for the gpio15: %s,%s,%s" %(gpio15_1,gpio15_2,gpio15_3))

            logfile=open("log.txt","a") #write info to windows txt file
            logfile.write("\r\n")
            logfile.write("."*44)
            logfile.write("\r\n")
            logfile.write(time.strftime("%m/%d/%Y"))
            logfile.write(" - ")
            logfile.write(time.strftime("%H:%M:%S"))
            logfile.write("\r\n")
            logfile.write("RPI CPU temperature: %s °C\r\n" %(cpu_temp))
            if gpio11 =="true":
                logfile.write("settings for the gpio11: %s,%s,%s\r\n" %(gpio11_1,gpio11_2,gpio11_3))
            if gpio13 =="true":
                logfile.write("settings for the gpio13: %s,%s,%s\r\n" %(gpio13_1,gpio13_2,gpio13_3))
            if gpio15 =="true":
                logfile.write("settings for the gpio15: %s,%s,%s\r\n" %(gpio15_1,gpio15_2,gpio15_3))
            logfile.close
the txt file looks like this:

Code: Select all

............................................
10/29/2014 - 08:00:00
RPI CPU temperature: 36.0 C
settings for the gpio11: 11,2000,10
settings for the gpio15: 15,1,100

............................................
10/29/2014 - 08:30:00
RPI CPU temperature: 36.0 C
settings for the gpio11: 11,2000,10
settings for the gpio15: 15,1,100

............................................
10/29/2014 - 09:00:00
RPI CPU temperature: 36.0 C
settings for the gpio11: 11,2000,10
settings for the gpio15: 15,1,100
edit:
this is a little bit different then the logging module ( I didn't know it excisted). But i did this mainly so i can check if my script didn't freeze. i can check this on the screen or acces the log file over the netwerk. it it didnt print a log file in the last 30 minutes i know something went wrong

Return to “Python”