def init(self, **kwargs): # Load the gloabl config if any log_config = dict(logger_defaults) log_config.update(config.get('log', {})) log_config.update(app_config.get('log', {})) log_config.update(config.get('log', {}).get(self._iz_name, {})) log_config.update(app_config.get('log', {}).get(self._iz_name, {})) log_config.update(kwargs) if log_config.get('level') and self._iz_handler: self._iz_handler.setLevel(log_config['level']) if log_config.get('filename') and not self._iz_filename: filename = paths.full_fpath(log_config['filename']) self._iz_handler = logging.FileHandler( filename=filename, mode=log_config.get('filemode', 'a'), encoding=log_config.get('fileencoding', 'utf8'), ) self._iz_handler.setLevel(logging.DEBUG) self.addHandler(self._iz_handler) if log_config.get('formatter') and self._iz_handler: self._iz_format = log_config['formatter'] self._iz_handler.setFormatter(log_config['formatter']) if log_config.get('format') and not self._iz_format: formatter = logging.Formatter( fmt=log_config['format'], datefmt=log_config.get('dateformat'), ) self._iz_handler.setFormatter(formatter) self._iz_format = log_config.get('format') if log_config.get('level') and self._iz_handler: self._iz_handler.setLevel(log_config['level'])
def sendmail(self, **kwargs): if config.get('debug') or self.options.get('debug'): msg = kwargs['msg'] log.info('DID NOT send email "{}" from "{}" to {}'.format( msg['Subject'], kwargs.get('from_addr'), msg['To'], )) log.debug(msg.as_string()) return log.info('Sent email "{}" from "{}" to {}'.format( kwargs['msg']['Subject'], kwargs.get('from_addr'), kwargs['msg']['To'], )) host = self.options.get('host', config.email.host) server = smtplib.SMTP(host) kwargs['msg'] = kwargs['msg'].as_string() server.sendmail(**kwargs) server.quit()
def template_parse(self, fpath, **tags): tags['config'] = config.dict() parsed_email = parse(fpath, **tags) if config.get('debug') or self.options.get('debug'): log.debug(u"---------\n" + parsed_email + "\n--------\n") return self.message_fromstr(parsed_email)
def initialize(**kwargs): request_initialize('config', **kwargs) # Setup the paths # Path configuration should be in the format: # paths: { # path: path to main data directory # # custom_path1: encoded path # ... # custom_pathN: encoded path # # } # Setup the paths first paths_config = config.get('paths', {}) paths_config.update(kwargs.get('paths', {})) if 'path' not in paths_config: for path_location in ['/tmp', '/temp', '.']: if os.path.isdir(path_location): paths_config.setdefault('path', path_location) break paths.initialize(**paths_config) # Setup the logger # Log configuration should be in the format: # log: { # filename: paths compatible filepath # filemode: usually 'a' # fileencoding: usually 'utf8' # format: logs formatter compatible format # dateformat: how to display dates # level: filter out everything above this level # # custom_logger_name1: { # filename: paths compatible filepath # filemode: usually 'a' # fileencoding: usually 'utf8' # format: logs formatter compatible format # dateformat: how to display dates # level: filter out everything above this level # }, # ... # custom_logger_nameN: { # filename: paths compatible filepath # filemode: usually 'a' # fileencoding: usually 'utf8' # format: logs formatter compatible format # dateformat: how to display dates # level: filter out everything above this level # }, # } # log_config = dict(logger_defaults) log_config.update(config.get('log', {})) log_config.update(kwargs.get('log', {})) # Default to app_dir/izaber.log if path not defined filename = paths.file_get('izaber.log') log_config.setdefault('filename', unicode(filename)) log_config['filename'] = paths.full_fpath(log_config['filename']) logging.basicConfig(**log_config) logging.getLogger().addHandler(logging.StreamHandler())