remove PLC.Debug.log, use PLC.Logger.logger instead newlogs
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Wed, 15 Feb 2017 11:05:51 +0000 (12:05 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Wed, 15 Feb 2017 11:05:51 +0000 (12:05 +0100)
no more old-fashioned print >> log construction
stuff goes into /var/log/plcapi.log
(requires a change in plc.d/httpd for initialization)

26 files changed:
Makefile
PLC/API.py
PLC/Accessor.py
PLC/Boot.py
PLC/Debug.py
PLC/Filter.py
PLC/LDAP.py
PLC/Logger.py [new file with mode: 0644]
PLC/Method.py
PLC/Methods/AddPersonToSite.py
PLC/Methods/AddSlice.py
PLC/Methods/GetBootMedium.py
PLC/Methods/GetNodeFlavour.py
PLC/Methods/GetPersons.py
PLC/Methods/RefreshPeer.py
PLC/Methods/ResetPassword.py
PLC/Methods/VerifyPerson.py
PLC/Peers.py
PLC/Persons.py
PLC/PostgreSQL.py
PLC/sendmail.py
apache/ModPython.py
apache/ModPythonJson.py
apache/plc.wsgi
doc/PLCAPI.xml.in
migrations/extract-views.py

index 3f61822..7f3d251 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -80,12 +80,13 @@ sync:
 ifeq (,$(SSHURL))
        @echo "sync: I need more info from the command line, e.g."
        @echo "  make sync PLC=boot.planetlab.eu"
-       @echo "  make sync PLCHOSTLXC=.. GUESTNAME=.."
+       @echo "  make sync PLCHOSTLXC=.. GUESTHOSTNAME=.. GUESTNAME=.."
        @exit 1
 else
        +$(RSYNC) plcsh PLC planetlab5.sql migrations aspects $(SSHURL)/usr/share/plc_api/
        +$(RSYNC) db-config.d/ $(SSHURL)/etc/planetlab/db-config.d/
        +$(RSYNC) plc.d/ $(SSHURL)/etc/plc.d/
+       +$(RSYNC) apache/plc.wsgi $(SSHURL)/usr/share/plc_api/apache/
        $(SSHCOMMAND) /etc/plc.d/httpd stop
        $(SSHCOMMAND) /etc/plc.d/httpd start
 endif
index f09034c..c40f28e 100644 (file)
@@ -141,6 +141,8 @@ class PLCAPI:
 
         # Load configuration
         self.config = Config(config)
+#        print("config has keys {}"
+#              .format(vars(self.config).keys()))
 
         # Initialize database connection
         if self.config.PLC_DB_TYPE == "postgresql":
index e8a69f5..7352f47 100644 (file)
@@ -9,7 +9,7 @@
 # by the Factory, you need to restart your python instance / web server
 # as the cached information then becomes wrong
 
-from PLC.Debug import log 
+from PLC.Logger import logger
 
 from PLC.TagTypes import TagTypes, TagType
 from PLC.Roles import Roles, Role
@@ -57,9 +57,9 @@ This is implemented as a singleton, so we can cache results over time"""
                     for rolename in roles_to_delete:
                         tag_type.remove_role(self.hash_name_to_role[rolename])
                 except:
-                    # this goes in boot.log ...
-                    print >> log, "WARNING, Could not enforce tag type, tagname=%s\n"%tagname
-                    traceback.print_exc(file=log)
+                    logger.exception("WARNING, Could not enforce tag type, tagname={}\n"
+                                     .format(tagname))
+
                     
         else:
             # not found: create it
@@ -74,7 +74,9 @@ This is implemented as a singleton, so we can cache results over time"""
                     tag_type.add_role(role_obj)
                 except:
                     # xxx todo find a more appropriate way of notifying this
-                    print >> log, "Accessor.locate_or_create_tag: Could not add role %r to tag_type %s"%(role,tagname)
+                    logger.exception("Accessor.locate_or_create_tag: "
+                                     "Could not add role {} to tag_type {}"
+                                     .format(role,tagname))
         self.set_cache(tagname,tag_type)
         return tag_type
 
index 52a9fd9..001211e 100644 (file)
@@ -6,7 +6,7 @@
 #
 
 from PLC.Faults import *
-from PLC.Debug import log
+from PLC.Logger import logger
 from PLC.Messages import Message, Messages
 from PLC.Persons import Person, Persons
 from PLC.Sites import Site, Sites
@@ -17,7 +17,7 @@ def notify_owners(method, node, message_id,
                   fault = None):
     messages = Messages(method.api, [message_id], enabled = True)
     if not messages:
-        print >> log, "No such message template '%s'" % message_id
+        logger.error("No such message template '%s'" % message_id)
         return 1
     message = messages[0]
 
index 27f9885..b120871 100644 (file)
@@ -3,18 +3,7 @@ import time
 import sys
 import syslog
 
-class unbuffered:
-    """
-    Write to /var/log/httpd/error_log. See
-
-    http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp
-    """
-
-    def write(self, data):
-        sys.stderr.write(data)
-        sys.stderr.flush()
-
-log = unbuffered()
+from PLC.Logger import logger
 
 def profile(callable):
     """
@@ -41,7 +30,7 @@ def profile(callable):
         end = time.time()
         args = map(str, args)
         args += ["%s = %s" % (name, str(value)) for (name, value) in kwds.items()]
-        print >> log, "%s (%s): %f s" % (callable.__name__, ", ".join(args), end - start)
+        logger.info("%s (%s): %f s" % (callable.__name__, ", ".join(args), end - start))
         return result
 
     return wrapper
index a991f78..d3a17b4 100644 (file)
@@ -6,6 +6,7 @@ import time
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter, Mixed, python_type
+from PLC.Logger import logger
 
 class Filter(Parameter, dict):
     """
@@ -266,5 +267,6 @@ class Filter(Parameter, dict):
         if clips:
             clip_part += " " + " ".join(clips)
         if Filter.debug:
-            print >> log, 'Filter.sql: where_part=',where_part,'clip_part',clip_part
-        return (where_part,clip_part)
+            logger.debug('Filter.sql: where_part={} - clip_part={}'
+                         .format(where_part, clip_part))
+        return where_part, clip_part
index bddc0e6..da54456 100644 (file)
@@ -6,7 +6,7 @@
 
 import ldap
 import traceback
-from PLC.Debug import profile, log
+from PLC.Debug import profile
 from PLC.Faults import *
 
 
diff --git a/PLC/Logger.py b/PLC/Logger.py
new file mode 100644 (file)
index 0000000..ca85ac9
--- /dev/null
@@ -0,0 +1,45 @@
+import logging
+import logging.config
+
+# we essentially need one all-purpose logger
+# that goes into /var/log/plcapi.log
+
+plcapi_logging_config = {
+    'version' : 1,
+    'disable_existing_loggers' : True,
+    'formatters': { 
+        'standard': { 
+            'format': '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s',
+            'datefmt': '%m-%d %H:%M:%S'
+        },
+        'shorter': { 
+            'format': '%(asctime)s %(levelname)s %(message)s',
+            'datefmt': '%d %H:%M:%S'
+        },
+    },
+    'handlers': {
+        'plcapi': {
+            'level': 'INFO',
+            'class': 'logging.FileHandler',
+            'formatter': 'standard',
+            'filename' : '/var/log/plcapi.log',
+        },
+    },
+    'loggers': {
+        'plcapi': {
+            'handlers': ['plcapi'],
+            'level': 'INFO',
+            'propagate': False,
+        },
+    },
+}
+
+logging.config.dictConfig(plcapi_logging_config)
+
+# general case:
+# from PLC.Logger import logger
+logger = logging.getLogger('plcapi')
+
+#################### test
+if __name__ == '__main__':
+    logger.info("in plcapi")
index 783bc5d..571c3eb 100644 (file)
@@ -16,7 +16,7 @@ from types import StringTypes
 from PLC.Faults import *
 from PLC.Parameter import Parameter, Mixed, python_type, xmlrpc_type
 from PLC.Auth import Auth
-from PLC.Debug import profile, log
+from PLC.Debug import profile
 from PLC.Events import Event, Events
 from PLC.Nodes import Node, Nodes
 from PLC.Persons import Person, Persons
index 17c3c99..9710f2b 100644 (file)
@@ -8,7 +8,7 @@ from PLC.PersonTags import PersonTags, PersonTag
 from PLC.Namespace import email_to_hrn
 from PLC.TagTypes import TagTypes
 
-from PLC.Debug import log
+from PLC.Logger import logger
 
 class AddPersonToSite(Method):
     """
@@ -65,12 +65,12 @@ class AddPersonToSite(Method):
         try:
             had_no_site= (len (person['site_ids']) == 0)
             if had_no_site: 
-                login_base=site['login_base']
+                login_base = site['login_base']
                 root_auth = self.api.config.PLC_HRN_ROOT
-                hrn=email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
-                tagname='hrn'
+                hrn = email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
+                tagname = 'hrn'
                 tag_type = TagTypes(self.api,{'tagname':tagname})[0]
-                person_tags=PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
+                person_tags = PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
                 if not person_tags:
                     person_tag = PersonTag(self.api)
                     person_tag['person_id'] = person['person_id']
@@ -82,11 +82,8 @@ class AddPersonToSite(Method):
                     person_tag = person_tags[0]
                     person_tag['value'] = hrn
                     person_tag.sync() 
-        except Exception,e:
-            print >> log, "BEG Warning, cannot maintain person's hrn, %s"%e
-            import traceback
-            traceback.print_exc(file=log)
-            print >> log, "END Warning, cannot maintain person's hrn, %s"%e
-                
+        except Exception as e:
+            logger.exception("ERROR cannot maintain person's hrn, {}"
+                             .format(person_id_or_email))
 
         return 1
index 51918f4..7542cf0 100644 (file)
@@ -13,7 +13,7 @@ from PLC.SliceTags import SliceTags
 from PLC.Methods.AddSliceTag import AddSliceTag
 from PLC.Methods.UpdateSliceTag import UpdateSliceTag
 
-from PLC.Debug import log
+from PLC.Logger import logger
 
 can_update = ['name', 'instantiation', 'url', 'description', 'max_nodes']
 
@@ -112,9 +112,7 @@ class AddSlice(Method):
             for value in values:
                 AddSliceTag(self.api).__call__(auth,slice['slice_id'],'vsys',value)
         except:
-            print >> log, "Could not set vsys tags as configured in PLC_VSYS_DEFAULTS"
-            import traceback
-            traceback.print_exc(file=log)
+            logger.exception("Could not set vsys tags as configured in PLC_VSYS_DEFAULTS")
         self.event_objects = {'Slice': [slice['slice_id']]}
         self.message = "Slice %d created" % slice['slice_id']
 
index 368ae1e..26b8372 100644 (file)
@@ -14,7 +14,7 @@ from PLC.Interfaces import Interface, Interfaces
 from PLC.InterfaceTags import InterfaceTag, InterfaceTags
 from PLC.NodeTags import NodeTag, NodeTags
 
-from PLC.Debug import log
+from PLC.Logger import logger
 
 from PLC.Accessors.Accessors_standard import *                  # import node accessors
 
@@ -295,7 +295,7 @@ class GetBootMedium(Method):
     def cleantrash (self):
         for file in self.trash:
             if self.DEBUG:
-                print >> log, 'DEBUG -- preserving',file
+                logger.debug('DEBUG -- preserving trash file {}'.format(file))
             else:
                 os.unlink(file)
 
@@ -362,7 +362,7 @@ class GetBootMedium(Method):
         # regular node, make build's arguments
         # and build the full command line to be called
         if node_type not in [ 'regular', 'reservable' ]:
-            print >> log, "GetBootMedium.build_command: unexpected node_type {}".format(node_type)
+            logger.error("GetBootMedium.build_command: unexpected node_type {}".format(node_type))
             return command, None
         
         build_sh_options=""
@@ -390,7 +390,7 @@ class GetBootMedium(Method):
                           build_sh_options,
                           log_file)
         
-        print >> log, "The build command line is {}".format(command)
+        logger.info("The build command line is {}".format(command))
 
         return command, log_file
 
@@ -415,8 +415,8 @@ class GetBootMedium(Method):
             raise PLCInvalidArgument("No such node {}".format(node_id_or_hostname))
         node = nodes[0]
 
-        print >> log, "GetBootMedium: {} requested on node {}. Node type is: {}"\
-            .format(action, node['node_id'], node['node_type'])
+        logger.info("GetBootMedium: {} requested on node {}. Node type is: {}"\
+            .format(action, node['node_id'], node['node_type']))
 
         # check the required action against the node type
         node_type = node['node_type']
@@ -600,10 +600,10 @@ class GetBootMedium(Method):
                     result = file(node_image).read()
                     self.trash.append(node_image)
                     self.cleantrash()
-                    print >> log, "GetBootMedium - done with build.sh"
+                    logger.info("GetBootMedium - done with build.sh")
                     encoded_result = base64.b64encode(result)
-                    print >> log, "GetBootMedium - done with base64 encoding - lengths: raw={} - b64={}"\
-                        .format(len(result), len(encoded_result))
+                    logger.info("GetBootMedium - done with base64 encoding - lengths: raw={} - b64={}"
+                                .format(len(result), len(encoded_result)))
                     return encoded_result
             except:
                 self.cleantrash()
index 30a7b8f..f357b2f 100644 (file)
@@ -1,5 +1,6 @@
 import traceback
 
+from PLC.Logger import logger
 from PLC.Method import Method
 from PLC.Auth import Auth
 from PLC.Faults import *
@@ -59,14 +60,13 @@ class GetNodeFlavour(Method):
             for assign in assigns:
                 (left,right)=[x.strip() for x in assign.split(':')]
                 if right not in GetNodeFlavour.known_virts:
-                    print >> log, "GetNodeFlavour, unknown 'virt' %s - ignored" % right
+                    logger.error("GetNodeFlavour, unknown 'virt' %s - ignored" % right)
                     continue
                 for fcdistro in [ x.strip() for x in left.split(',')]:
                     map[fcdistro]=right
         except:
-            print >> log, "GetNodeFlavour, issue with parsing PLC_FLAVOUR_VIRT_MAP=%s - returning '%s'"%\
-                (self.api.config.PLC_FLAVOUR_VIRT_MAP,GetNodeFlavour.default_virt)
-            traceback.print_exc(file=log)
+            logger.exception("GetNodeFlavour, issue with parsing PLC_FLAVOUR_VIRT_MAP=%s - returning '%s'"%\
+                             (self.api.config.PLC_FLAVOUR_VIRT_MAP, GetNodeFlavour.default_virt))
             return GetNodeFlavour.default_virt
 #        print 'virt_from_virt_map, using map',map
         if node_fcdistro in map:  return map[node_fcdistro]
index 06da9f5..263c663 100644 (file)
@@ -5,6 +5,7 @@ from PLC.Filter import Filter
 from PLC.Persons import Person, Persons
 from PLC.Sites import Site, Sites
 from PLC.Auth import Auth
+from PLC.Logger import logger
 
 hidden_fields = ['password', 'verification_key', 'verification_expires']
 
@@ -39,6 +40,10 @@ class GetPersons(Method):
     returns = [return_fields]
 
     def call(self, auth, person_filter = None, return_fields = None):
+
+        logger.info("incoming GetPersons, filter={}, return fields={}"
+                    .format(person_filter, return_fields))
+
         # If we are not admin, make sure to only return viewable accounts
         if isinstance(self.caller, Person) and \
            'admin' not in self.caller['roles']:
index 90a0b70..0eb36e6 100644 (file)
@@ -6,7 +6,7 @@ import sys
 import fcntl
 import time
 
-from PLC.Debug import log
+from PLC.Logger import logger
 from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
@@ -52,15 +52,13 @@ focus_ids=[]    # set to a list of ids (e.g. person_ids) - remote or local ids s
 #focus_ids=[621,1088]
 
 #################### helpers
-def message (to_print=None,verbose_only=False):
+def message(to_print=None, verbose_only=False):
     if verbose_only and not verbose:
         return
-    print >> log, time.strftime("%m-%d-%H-%M-%S:"),
-    if to_print:
-        print >>log, to_print
+    logger.info(to_print)
 
 def message_verbose(to_print=None, header='VERBOSE'):
-    message("%s> %r"%(header,to_print),verbose_only=True)
+    message("%s> %r"%(header, to_print), verbose_only=True)
 
 
 #################### to avoid several instances running at the same time
@@ -227,7 +225,7 @@ class RefreshPeer(Method):
                 return peer_object_id in focus_ids or \
                     (object and primary_key in object and object[primary_key] in focus_ids)
 
-            def message_focus (message):
+            def message_focus(message):
                 if in_focus():
                     # always show remote
                     message_verbose("peer_obj : %d [[%r]]"%(peer_object_id,peer_object),
@@ -250,19 +248,17 @@ class RefreshPeer(Method):
                     return True
                 else:
                     result=True
-#                    print >> log, 'COMPARING ',
                     for column in columns:
                         test= object[column] == peer_object[column]
-#                        print >> log, column,test,
                         if not test: result=False
-#                    print >> log, '=>',result
                     return result
 
             # Add/update new/existing objects
             for peer_object_id, peer_object in peer_objects.iteritems():
                 peer_object_name=""
                 if secondary_key: peer_object_name="(%s)"%peer_object[secondary_key]
-                message_verbose ('%s peer_object_id=%d %s (%d/%d)'%(classname,peer_object_id,peer_object_name,count,total))
+                message_verbose('%s peer_object_id=%d %s (%d/%d)'
+                                %(classname,peer_object_id,peer_object_name,count,total))
                 count += 1
                 if peer_object_id in synced:
                     message("Warning: %s Skipping already added %s: %r"%(
@@ -281,11 +277,11 @@ class RefreshPeer(Method):
                     if not equal_fields(object,peer_object, columns):
                         # Only update intrinsic fields
                         object.update(object.db_fields(peer_object))
-                        message_focus ("DIFFERENCES : updated / syncing")
+                        message_focus("DIFFERENCES : updated / syncing")
                         sync = True
                         action = "changed"
                     else:
-                        message_focus ("UNCHANGED - left intact / not syncing")
+                        message_focus("UNCHANGED - left intact / not syncing")
                         sync = False
                         action = None
 
@@ -297,26 +293,28 @@ class RefreshPeer(Method):
                     object = classobj(self.api, peer_object)
                     # Replace foreign identifier with new local identifier
                     del object[primary_key]
-                    message_focus ("NEW -- created with clean id - syncing")
+                    message_focus("NEW -- created with clean id - syncing")
                     sync = True
                     action = "added"
 
                 if sync:
-                    message_verbose("syncing %s %d - commit_mode=%r"%(classname,peer_object_id,commit_mode))
+                    message_verbose("syncing %s %d - commit_mode=%r"
+                                    %(classname,peer_object_id,commit_mode))
                     try:
                         object.sync(commit = commit_mode)
                     except PLCInvalidArgument, err:
                         # Skip if validation fails
                         # XXX Log an event instead of printing to logfile
-                        message("Warning: %s Skipping invalid %s %r : %r"%(\
-                                peer['peername'], classname, peer_object, err))
+                        message("Warning: %s Skipping invalid %s %r : %r"%
+                                (peer['peername'], classname, peer_object, err))
                         continue
 
                 synced[peer_object_id] = object
 
                 if action:
-                    message("%s: (%d/%d) %s %d %s %s"%(peer['peername'], count,total, classname, 
-                                                       object[primary_key], peer_object_name, action))
+                    message("%s: (%d/%d) %s %d %s %s"
+                            %(peer['peername'], count,total, classname, 
+                              object[primary_key], peer_object_name, action))
 
             message_verbose("Exiting sync on %s"%classname)
 
@@ -353,7 +351,8 @@ class RefreshPeer(Method):
         sites_at_peer = dict([(site['site_id'], site) for site in peer_tables['Sites']])
 
         # Synchronize new set (still keyed on foreign site_id)
-        peer_sites = sync(old_peer_sites, sites_at_peer, Site, ignore (columns, RefreshPeer.ignore_site_fields))
+        peer_sites = sync(old_peer_sites, sites_at_peer, Site,
+                          ignore(columns, RefreshPeer.ignore_site_fields))
 
         for peer_site_id, site in peer_sites.iteritems():
             # Bind any newly cached sites to peer
@@ -398,7 +397,8 @@ class RefreshPeer(Method):
                 continue
 
         # Synchronize new set (still keyed on foreign key_id)
-        peer_keys = sync(old_peer_keys, keys_at_peer, Key, ignore (columns, RefreshPeer.ignore_key_fields))
+        peer_keys = sync(old_peer_keys, keys_at_peer, Key,
+                         ignore(columns, RefreshPeer.ignore_key_fields))
         for peer_key_id, key in peer_keys.iteritems():
             # Bind any newly cached keys to peer
             if peer_key_id not in old_peer_keys:
@@ -436,7 +436,8 @@ class RefreshPeer(Method):
         # XXX Do we care about membership in foreign site(s)?
 
         # Synchronize new set (still keyed on foreign person_id)
-        peer_persons = sync(old_peer_persons, persons_at_peer, Person, ignore (columns, RefreshPeer.ignore_person_fields))
+        peer_persons = sync(old_peer_persons, persons_at_peer, Person,
+                            ignore(columns, RefreshPeer.ignore_person_fields))
 
         # transcoder : retrieve a local key_id from a peer_key_id
         key_transcoder = dict ( [ (key['key_id'],peer_key_id) \
index 967f21f..8e9da53 100644 (file)
@@ -5,7 +5,7 @@ import urllib
 
 from types import StringTypes
 
-from PLC.Debug import log
+from PLC.Logger import logger
 from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
@@ -119,7 +119,7 @@ class ResetPassword(Method):
                      Subject = message['subject'] % params,
                      Body = message['template'] % params)
         else:
-            print >> log, "Warning: No message template '%s'" % message_id
+            logger.warning("No message template '%s'" % message_id)
 
         # Logging variables
         self.event_objects = {'Person': [person['person_id']]}
index a0fc304..b34506a 100644 (file)
@@ -3,7 +3,7 @@ import base64
 import time
 import urllib
 
-from PLC.Debug import log
+from PLC.Logger import logger
 from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
@@ -143,7 +143,7 @@ class VerifyPerson(Method):
                      Subject = message['subject'] % params,
                      Body = message['template'] % params)
         else:
-            print >> log, "Warning: No message template '%s'" % message_id
+            logger.warning("No message template '%s'" % message_id)
 
         # Logging variables
         self.event_objects = {'Person': [person['person_id']]}
index bab43f6..c272a2a 100644 (file)
@@ -8,7 +8,7 @@ import traceback
 from urlparse import urlparse
 
 import PLC.Auth
-from PLC.Debug import log
+from PLC.Logger import logger
 from PLC.Faults import *
 from PLC.Namespace import hostname_to_hrn
 from PLC.Parameter import Parameter, Mixed
@@ -175,9 +175,7 @@ class Peer(Row):
             tags = {'hrn': hrn}
             Node(self.api, node).update_tags(tags)
         except:
-            print >> log, "WARNING: (beg) could not find out hrn on hostname=%s"%node['hostname']
-            traceback.print_exc(5,file=log)
-            print >> log, "WARNING: (end) could not find out hrn on hostname=%s"%node['hostname']
+            logger.exception("Could not find out hrn on hostname=%s"%node['hostname'])
 
     def remove_node(self, node, commit = True):
         """
index 8276c06..8cd2856 100644 (file)
@@ -16,7 +16,6 @@ import re
 import crypt
 
 from PLC.Faults import *
-from PLC.Debug import log
 from PLC.Parameter import Parameter, Mixed
 from PLC.Filter import Filter
 from PLC.Table import Row, Table
index 1dfced5..e04b02b 100644 (file)
@@ -19,7 +19,8 @@ import commands
 import re
 from pprint import pformat
 
-from PLC.Debug import profile, log
+from PLC.Logger import logger
+from PLC.Debug import profile
 from PLC.Faults import *
 from datetime import datetime as DateTimeType
 
@@ -175,21 +176,22 @@ class PostgreSQL:
 
             if not params:
                 if self.debug:
-                    print >> log,'execute0',query
+                    logger.debug('execute0: {}'.format(query))
                 cursor.execute(query)
-            elif isinstance(params,dict):
+            elif isinstance(params, dict):
                 if self.debug:
-                    print >> log,'execute-dict: params',params,'query',query%params
-                cursor.execute(query,params)
+                    logger.debug('execute-dict: params {} query {}'
+                                 .format(params, query%params))
+                cursor.execute(query, params)
             elif isinstance(params,tuple) and len(params)==1:
                 if self.debug:
-                    print >> log,'execute-tuple',query%params[0]
+                    logger.debug('execute-tuple {}'.format(query%params[0]))
                 cursor.execute(query,params[0])
             else:
                 param_seq=(params,)
                 if self.debug:
                     for params in param_seq:
-                        print >> log,'executemany',query%params
+                        logger.debug('executemany {}'.format(query%params))
                 cursor.executemany(query, param_seq)
             (self.rowcount, self.description, self.lastrowid) = \
                             (cursor.rowcount, cursor.description, cursor.lastrowid)
@@ -199,12 +201,8 @@ class PostgreSQL:
             except:
                 pass
             uuid = commands.getoutput("uuidgen")
-            print >> log, "Database error %s:" % uuid
-            print >> log, e
-            print >> log, "Query:"
-            print >> log, query
-            print >> log, "Params:"
-            print >> log, pformat(params)
+            message = "Database error {}: - Query {} - Params {}".format(uuid, query, pformat(params))
+            logger.exception(message)
             raise PLCDBError("Please contact " + \
                              self.api.config.PLC_NAME + " Support " + \
                              "<" + self.api.config.PLC_MAIL_SUPPORT_ADDRESS + ">" + \
index cab0ae6..a5f0c5e 100644 (file)
@@ -6,7 +6,7 @@ from email.MIMEText import MIMEText
 from email.Header import Header
 from smtplib import SMTP
 
-from PLC.Debug import log
+from PLC.Logger import logger
 from PLC.Faults import *
 
 def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None):
@@ -86,7 +86,8 @@ def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None):
     msg['X-Mailer'] = "Python/" + sys.version.split(" ")[0]
 
     if not api.config.PLC_MAIL_ENABLED:
-        print >> log, "From: %(From)s, To: %(To)s, Subject: %(Subject)s" % msg
+        logger.info("PLC_MAIL_ENABLED not set")
+        logger.info("From: %(From)s, To: %(To)s, Subject: %(Subject)s" % msg)
         return
 
     s = SMTP()
index 07a0fda..c091894 100644 (file)
@@ -13,7 +13,7 @@ import traceback
 import xmlrpclib
 from mod_python import apache
 
-from PLC.Debug import log
+from PLC.Logger import logger
 
 from PLC.API import PLCAPI
 api = PLCAPI()
@@ -56,8 +56,6 @@ def handler(req):
 
         return apache.OK
 
-    except Exception, err:
-        # Log error in /var/log/httpd/(ssl_)?error_log
-        t = "[" + time.ctime() + "] [error]"
-        print >> log, t, err, traceback.format_exc()
+    except Exception as err:
+        logger.exception("INTERNAL ERROR !!")
         return apache.HTTP_INTERNAL_SERVER_ERROR
index 22dbdc7..78cb363 100644 (file)
@@ -12,7 +12,7 @@ import traceback
 import xmlrpclib
 from mod_python import apache
 
-from PLC.Debug import log
+from PLC.Logger import logger
 
 from PLC.API import PLCAPI
 api = PLCAPI()
@@ -56,6 +56,5 @@ def handler(req):
         return apache.OK
 
     except Exception, err:
-        # Log error in /var/log/httpd/(ssl_)?error_log
-        print >> log, err, traceback.format_exc()
+        logger.exception("INTERNAL ERROR !!")
         return apache.HTTP_INTERNAL_SERVER_ERROR
index c37d369..3e0d51c 100644 (file)
@@ -9,7 +9,7 @@ import sys
 sys.path.append('/usr/share/plc_api')
 sys.stdout = sys.stderr
 import traceback
-from PLC.Debug import log
+from PLC.Logger import logger
 from PLC.API import PLCAPI
 
 def application(environ, start_response):
@@ -40,11 +40,11 @@ def application(environ, start_response):
             # Shut down database connection, otherwise up to MaxClients DB
             # connections will remain open.
             api.db.close()
-    except Exception, err:
+    except Exception as err:
         status = '500 Internal Server Error'
         content_type = 'text/html'
         output = 'Internal Server Error'
-        print >> log, err, traceback.format_exc()
+        logger.exception("INTERNAL ERROR !!")
 
     # Write response
     response_headers = [('Content-type', '%s' % content_type),
index 79fb1a8..02ed262 100644 (file)
@@ -559,9 +559,9 @@ node_ids = plc_api.GetSlices(auth,slice_name,['node_ids'])[0]['node_ids']
 slice_nodes = plc_api.GetNodes(auth,node_ids,['hostname'])
 
 # store in a file
-f=open('mynodes.txt','w')
-for node in slice_nodes:
-    print >>f,node['hostname']
+with ('mynodes.txt','a') as f:
+    for node in slice_nodes:
+        f.write(node['hostname'] + "\n")
 f.close()
 </programlisting>
   </section>
index 0daed51..4fd5090 100755 (executable)
@@ -16,7 +16,7 @@ class Schema:
 
     def parse (self):
         if self.output:
-            outfile = open(self.output,"w")
+            outfile = open(self.output, "a")
         else:
             outfile = sys.stdout
         contents = file(self.input).read()
@@ -40,7 +40,7 @@ class Schema:
                 out_line=match.group(1)
             match=Schema.view.match(out_line)
             if match:
-                print >>outfile, out_line,';'
+                outfile.write("{};\n".format(out_line))
         if outfile != sys.stdout:
             outfile.close()