diff eprodbus.py @ 15:08b61687b75f

Use logger better
author Daniel O'Connor <darius@dons.net.au>
date Mon, 06 Dec 2021 11:27:11 +1030
parents 446cfe74827b
children 3338e666256b
line wrap: on
line diff
--- a/eprodbus.py	Mon Dec 06 11:26:20 2021 +1030
+++ b/eprodbus.py	Mon Dec 06 11:27:11 2021 +1030
@@ -17,8 +17,23 @@
 sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'velib_python'))
 from vedbus import VeDbusService
 
-logging.basicConfig(format = '%(asctime)s %(message)s', level = logging.DEBUG)
-logging.info(__file__ + " is starting up")
+logger = logging.getLogger('eprodbus')
+logger.setLevel(logging.INFO)
+
+formatter = logging.Formatter('%(asctime)s - %(message)s')
+
+#fh = logging.FileHandler('/var/log/eprodbus.log')
+#fh.setLevel(logging.DEBUG)
+#fh.setFormatter(formatter)
+
+ch = logging.StreamHandler()
+ch.setLevel(logging.INFO)
+ch.setFormatter(formatter)
+
+#logger.addHandler(fh)
+logger.addHandler(ch)
+
+logger.info(__file__ + " is starting up")
 
 port = 'ttyepro'
 servicename = 'com.victronenergy.battery.' + port
@@ -38,17 +53,17 @@
         try:
             data = self.s.read(1024)
         except Exception as e:
-            logging.error('Failed to read from serial port: %s', str(e))
+            logger.error('Failed to read from serial port: %s', str(e))
             return False
 
-        logging.debug('Read %d bytes from serial port', len(data))
+        logger.debug('Read %d bytes from serial port', len(data))
         self.p.process(data)
 
         while len(self.p.packets) > 0:
             # Process oldest packets first
             p = self.p.packets.pop(0)
             self.log_queue.append(p)
-            logging.debug('%s', str(p))
+            logger.debug('%s', str(p))
             if type(p) == epro.StateOfCharge:
                 self.dbusservice['/Soc'] = p.soc
             elif type(p) == epro.MainVoltage:
@@ -63,7 +78,7 @@
         return True
 
     def log_epro(self):
-        logging.debug('Logging epro data')
+        logger.debug('Logging epro data')
         # Check we have all the packets we need in the queue
         msgtypes = set([x.msgtype for x in self.log_queue])
         wantedtypes = set([
@@ -77,7 +92,7 @@
             epro.AuxVoltage.MSGTYPE,
             ])
         if msgtypes < wantedtypes:
-            logging.debug('Didn\'t get all packet types required to log')
+            logger.debug('Didn\'t get all packet types required to log')
             return
 
         row = {}
@@ -123,7 +138,7 @@
                 self.log_queue = []
                 break
 
-        logging.info('Got all packets, logging')
+        logger.info('Got all packets, logging')
         cur = self.dbh.cursor()
         row['tstamp'] = int(datetime.datetime.now().strftime('%s'))
         cur.execute('INSERT INTO eprolog VALUES (:tstamp, :main_voltage, :aux_voltage, :battery_curr, :amp_hours, :state_of_charge, :time_remaining, :battery_temp, :auto_sync_volts, :auto_sync_curr, :e501, :alarm_test, :light, :display_test, :temp_sensor, :aux_hv, :aux_lv, :installer_lock, :main_hv, :main_lv, :low_battery, :battery_flat, :battery_full, :battery_charged, :no_sync, :monitor_reset)', row)
@@ -157,7 +172,7 @@
 
     updater = eProUpdater(dbusservice, s, dbh)
 
-    logging.info('Starting main loop')
+    logger.info('Starting main loop')
     mainloop = gobject.MainLoop()
     mainloop.run()