def __init__(self, name, cluster='', arclog=True): self.conf = aCTConfig.aCTConfigARC() self.logger = logging.LoggerAdapter(logging.getLogger(name), {'cluster': cluster}) self.logger.logger.setLevel(logging.DEBUG) level = LEVELS.get(self.conf.get(["logger", "level"]), logging.NOTSET) logfile = os.path.join(self.conf.get(["logger", "logdir"]), name + '.log') self.logger.logger.setLevel(level) # aCTMain calls logrotate to rotate logs self.handler = logging.handlers.WatchedFileHandler(logfile) if cluster: self.formatter = logging.Formatter( "[%(asctime)s] [%(filename)s:%(lineno)d] [%(levelname)s] [%(cluster)s] - %(message)s" ) else: self.formatter = logging.Formatter( "[%(asctime)s] [%(filename)s:%(lineno)d] [%(levelname)s] - %(message)s" ) self.handler.setFormatter(self.formatter) self.logger.logger.addHandler(self.handler) if arclog: self.arclogfile = arc.LogFile(str(logfile)) self.arclogfile.setFormat(arc.LongFormat) arc.Logger_getRootLogger().addDestination(self.arclogfile) if self.conf.get(["logger", "arclevel"]): arc.Logger_getRootLogger().setThreshold( arc.string_to_level( str(self.conf.get(["logger", "arclevel"])).upper())) else: arc.Logger_getRootLogger().setThreshold(arc.ERROR)
def __init__(self, config_path=None, log=sys.stdout, log_level=LogLevels.INFO): """ Create an object to interface with the ARC server. :param config_path: Path to config JSON file, or ``None`` to use the default settings :param log: File-like object to write log messages to, or ``None`` to disable logging. Use ``sys.stdout`` or ``sys.stderr`` to print messages (default: ``sys.stdout``). :param log_level: The level of detail logs should show (default: `LogLevels.INFO`). See `LogLevels` for the available levels :raises InvalidConfigError: if config is not valid JSON or is otherwise invalid """ self.logger = arc.Logger(arc.Logger_getRootLogger(), "jobsubmit") # Add a log destination if the user has provided one if log: log_dest = arc.LogStream(log) log_dest.setFormat(arc.ShortFormat) arc.Logger_getRootLogger().addDestination(log_dest) arc.Logger_getRootLogger().setThreshold(log_level.value) config_dict = {} if config_path: try: self.logger.msg( arc.DEBUG, "Using jasmin_arc config: {}".format(config_path)) # Let errors reading file bubble to calling code with open(config_path) as config_file: config_dict = json.load(config_file) # Catch JSON parsing errors except ValueError as e: raise InvalidConfigError(e.message) self.config = ConnectionConfig(config_dict, logger=self.logger) # Create jinja2 environment for loading JSDL template(s) self.env = Environment(loader=PackageLoader(__name__, TEMPLATES_DIR), autoescape=select_autoescape(["xml"])) self.cached_user_config = None
def __init__(self, baselogger, workerid): '''Set up ARC logging to log to the same file as the baselogger''' # Set method name to the caller of this method self.log = core_utils.make_logger( baselogger, token='workerID={0}'.format(workerid), method_name=inspect.stack()[1][3]) # Get the log file from the baseLogger loghandler = baselogger.handlers[0] # Assumes one handler # LogFile must exist for the lifetime of this object self.logfile = arc.LogFile(str(loghandler.baseFilename)) self.logfile.setFormat(arc.LongFormat) arc.Logger_getRootLogger().setThreadContext() arc.Logger_getRootLogger().addDestination(self.logfile) arc.Logger_getRootLogger().setThreshold( arc.VERBOSE) # TODO configurable
def _log(level, message, caller=None): """ Print a message to the log. This method is usually wrapped by the above. :param int level: log level :param str message: log message :param str caller: name of function """ if caller: caller = 'PythonLRMS.%s' % caller else: caller = 'PythonLRMS' arc.Logger(arc.Logger_getRootLogger(), caller).msg(level, message)
def __init__(self): super(DTRGenerator, self).__init__() # Set up logging self.root_logger = arc.Logger_getRootLogger() self.stream = arc.LogStream(sys.stdout) self.root_logger.addDestination(self.stream) self.root_logger.setThreshold(arc.DEBUG) self.cfg = arc.UserConfig('', '') self.id = '1' arc.DTR.LOG_LEVEL = self.root_logger.getThreshold() # Start the Scheduler self.scheduler = arc.Scheduler() self.scheduler.start()
import arc import time logger = arc.Logger(arc.Logger_getRootLogger(), 'EchoService.py') wsrf_rp_ns = "http://docs.oasis-open.org/wsrf/rp-2" echo_ns = "http://www.nordugrid.org/schemas/echo" import threading class EchoService(object): def __init__(self, cfg): logger.msg(arc.INFO, "EchoService (python) constructor called") # get the response-prefix from the config XML self.prefix = str(cfg.Get('prefix')) # get the response-suffix from the config XML self.suffix = str(cfg.Get('suffix')) logger.msg( arc.DEBUG, "EchoService (python) has prefix %(prefix)s and suffix %(suffix)s" % { 'prefix': self.prefix, 'suffix': self.suffix }) self.ssl_config = self.parse_ssl_config(cfg) thread_test = str(cfg.Get('ThreadTest')) if thread_test: threading.Thread(target=self.infinite, args=[thread_test]).start() def __del__(self): logger.msg(arc.INFO, "EchoService (python) destructor called")
import arc import sys # Set up logging to stderr with level VERBOSE (a lot of output will be shown) logstdout = arc.LogStream(sys.stdout) logstdout.setFormat(arc.ShortFormat) arc.Logger_getRootLogger().addDestination(logstdout) arc.Logger_getRootLogger().setThreshold(arc.VERBOSE) logger = arc.Logger(arc.Logger_getRootLogger(), "jobsubmit") # UserConfig contains information on credentials and default services to use. # This form of the constructor is necessary to initialise the local job list. usercfg = arc.UserConfig("", "") # Simple job description which outputs hostname to stdout jobdescstring = "&(executable=/bin/hostname)(stdout=stdout)" # Parse job description jobdescs = arc.JobDescriptionList() if not arc.JobDescription_Parse(jobdescstring, jobdescs): logger.msg(arc.ERROR, "Invalid job description") sys.exit(1) # Use 'arc.JobDescription_ParseFromFile("helloworld.xrsl", jobdescs)' # to parse job description from file. # Use top-level NorduGrid information index to find resources index = arc.Endpoint( "ldap://index1.nordugrid.org:2135/Mds-Vo-name=NorduGrid,o=grid", arc.Endpoint.REGISTRY, "org.nordugrid.ldapegiis") services = arc.EndpointList(1, index)
#! /usr/bin/env python from __future__ import print_function import arc import sys root_logger = arc.Logger_getRootLogger() root_logger.addDestination(arc.LogStream(sys.stdout)) root_logger.setThreshold(arc.ERROR) if len(sys.argv) < 2: print("Usage: echo_client.py URL [message]") print( " echo_client gets the credentials from the default user config file") sys.exit(-1) url = arc.URL(sys.argv[1]) try: message = sys.argv[2] except: message = 'hi!' cfg = arc.MCCConfig() uc = arc.UserConfig('') uc.ApplyToConfig(cfg) s = arc.ClientSOAP(cfg, url) outpayload = arc.PayloadSOAP( arc.NS('echo', 'http://www.nordugrid.org/schemas/echo')) outpayload.NewChild('echo:echo').NewChild('echo:say').Set(message) resp, status = s.process(outpayload) print(resp.GetXML(True))
def __del__(self): ''' Since self.logfile disappears when this object is deleted we have to remove it from the root logger destinations ''' arc.Logger_getRootLogger().removeDestinations()