vmoa / imagelib

RFO Image Library
0 stars 0 forks source link

Adopt a common logging library #3

Open georgeloyer opened 1 month ago

georgeloyer commented 1 month ago

Be nice to retrofit fitsimport to use this as well. It currently prints to stdout/stderr and relies on shell redirection to capture logs. I’ll take that as a feature request, should be straightforward. The logging library I used for the powerpi has worked well for that project.

georgeloyer commented 1 month ago

Here's the logging solution for rfopower:

import os import time ... RFO_LOG = '{pathname to log file}' ... def rfo_print_log(log_string): """ print log strings to a standard log file """ try: with open(RFO_LOG, "a") as logfile: # append only to the file logfile.write(time.strftime("%Y-%m-%d %H:%M:%S")) # print the timestamp logfile.write(" ") logfile.write("[") # print the pid in brackets logfile.write(str(os.getpid())) logfile.write("] ") logfile.write(log_string) # print string to log file logfile.write("\n") # print a newline except IOError as err_str: # catch IO errors print ("Error opening log file") # must write errors to STDOUT print (err_str)

... Example of using this function with errors from other libraries: def wait_for_internet(): """ waiting for access to the internet """ while True: try: response = urllib.request.urlopen('http://www.google.com', data=None, timeout=2) return except urllib.error.URLError as err_str: rfo_print_log(str(err_str.reason)) time.sleep(8)

... Example of using this function to show milestones in the log file:

reporting the start of the app in the log

rfo_print_log("STARTING RFOPOWER")