Exemplo n.º 1
0
def setup():
    formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.addHandler(handler)

    if "VERBOSE_TEST" in os.environ:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.ERROR)

    logger = logging.getLogger("requests.packages.urllib3.connectionpool")
    logger.setLevel(logging.ERROR)

    logger = logging.getLogger("cryptoassets.core.backend.blockio")
    logger.setLevel(logging.DEBUG)

    logger = logging.getLogger("cryptoassets.core.backend.bitcoind")
    logger.setLevel(logging.WARN)

    # SQL Alchemy transactions
    logger = logging.getLogger("txn")
    logger.setLevel(logging.ERROR)
Exemplo n.º 2
0
def setup_console_logging(log_level=None):
    """Setup console logging.

    Aimed to give easy sane defaults for loggig in command line applications.

    Don't use logging settings from INI, but use hardcoded defaults.
    """

    formatter = logging.Formatter("[%(asctime)s] [%(name)s %(funcName)s] %(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stdout)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.handlers = [handler]

    env_level = os.environ.get("LOG_LEVEL", "info")
    log_level = log_level or getattr(logging, env_level.upper())
    logger.setLevel(log_level)

    logger = logging.getLogger("requests.packages.urllib3.connectionpool")
    logger.setLevel(logging.ERROR)

    # SQL Alchemy transactions
    logger = logging.getLogger("txn")
    logger.setLevel(logging.ERROR)
Exemplo n.º 3
0
def setup_custom_logger():
    LOG_FILE = 'log/cpa'

    formatter = logging.Formatter("[%(asctime)s] %(threadName)s - %(pathname)s %(funcName)s():%(lineno)d  %(levelname)s \t%(message)s")  # same as default
    # formatter = logging.Formatter('%(asctime)s [%(pathname)s: %(lineno)d] %(levelname)s %(message)s')

    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    file_handler = ParallelTimedRotatingFileHandler(LOG_FILE, when='H')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    # console_handler = logging.StreamHandler(sys.stdout)
    # console_handler.formatter = formatter
    # logger.addHandler(console_handler)

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    #Add file handler
    handler = TimedRotatingFileHandler(LOG_FILE,
                                       when="h",
                                       interval=1,
                                       backupCount=48)

    # try:
    #     raise RuntimeError("Opa!")
    # except Exception as e:
    #     logger.exception(e)

    return logger
Exemplo n.º 4
0
def test_format_all_column():
    logger = logging.getLogger('test_logging_format_all_column')
    logger.setLevel(logging.DEBUG)

    formatter = logging.Formatter('''
        name            = '%(name)s'
        levelno         = '%(levelno)s'
        levelname       = '%(levelname)s'
        pathname        = '%(pathname)s'
        filename        = '%(filename)s'
        module          = '%(module)s'
        lineno          = '%(lineno)d'
        funcName        = '%(funcName)s'
        created         = '%(created)f'
        asctime         = '%(asctime)s'
        msecs           = '%(msecs)d'
        relativeCreated = '%(relativeCreated)d'
        thread          = '%(thread)d'
        threadName      = '%(threadName)s'
        process         = '%(process)d'
        message         = '%(message)s'
    ''')

    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.debug("debug msg")
Exemplo n.º 5
0
def setup_logging() -> None:
    """
    Sets up the logging module to have a verbose option and formats the Console handler and File handler
    """
    logging.addLevelName(VERBOSE, "VERBOSE")
    logging.Logger.verbose = logger_verbose
    logging.verbose = logging_verbose
    logging.VERBOSE = VERBOSE

    # define console handler
    console_handler = ConsoleHandler(sys.stderr)
    if hasattr(console_handler, "_column_color"):
        # noinspection PyProtectedMember
        # pylint: disable=protected-access
        console_handler._column_color['%(message)s'][logging.VERBOSE] = ('cyan', None, False)

    console_formatter = logging.Formatter("[%(asctime)s] %(levelname)s : %(message)s", "%H:%M:%S")
    console_handler.setFormatter(console_formatter)
    console_handler.setLevel(logging.VERBOSE)

    # define file handler
    if not os.path.exists(os.path.dirname(get_global_conf().getdir("install", "log_file"))):
        os.makedirs(os.path.dirname(get_global_conf().getdir("install", "log_file")))
    file_handler = logging.FileHandler(get_global_conf().getdir("install", "log_file"))

    # add handlers
    logging.getLogger().addHandler(console_handler)
    logging.getLogger().addHandler(file_handler)

    logging.getLogger().setLevel(get_global_conf().getint("install", "log_level"))
Exemplo n.º 6
0
def setup_logging():
    log = logging.getLogger("")
    handler = RainbowLoggingHandler(sys.stderr)
    handler._column_color['%(asctime)s'] = ('cyan', None, False)
    handler._column_color['%(levelname)-7s'] = ('green', None, False)
    handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
    handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
    log.addHandler(handler)
    log.setLevel(logging.INFO)
Exemplo n.º 7
0
def logger(config_file):
    CONFIG = configparser.RawConfigParser()

    CONFIG.read(config_file)

    logger_config = CONFIG['logger']

    logger = logging.getLogger(logger_config['name'])

    logger.setLevel(LOGGING_LEVELS.get(logger_config['level'], logging.NOTSET))

    formatter = logging.Formatter(
        '[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s'
    )

    handler = RainbowLoggingHandler(
        sys.stderr,
        color_funcName=('black', 'yellow', True)
    )

    handler.setFormatter(formatter)

    logger.addHandler(handler)

    if CONFIG.has_section('file_logger'):
        file_logger_config = CONFIG['file_logger']

        fh = logging.handlers.RotatingFileHandler(
            file_logger_config['file_path'],
            maxBytes=int(file_logger_config['max_log_file_size']),
            backupCount=int(file_logger_config['backup_count'])
        )

        fh.setLevel(LOGGING_LEVELS.get(file_logger_config['level'],
                                       logging.NOTSET))
        fh.setFormatter(formatter)
        logger.addHandler(fh)

    if CONFIG.has_section('email_logger'):
        email_logger_config = CONFIG['email_logger']

        eh = logging.handlers.SMTPHandler(
            (email_logger_config['host'], email_logger_config['port']),
            email_logger_config['from'],
            email_logger_config['to'],
            email_logger_config['subject'],
            (email_logger_config['username'], email_logger_config['password']),
            ()
        )

        eh.setLevel(LOGGING_LEVELS.get(email_logger_config['level'],
                                       logging.NOTSET))
        eh.setFormatter(formatter)
        logger.addHandler(eh)

    return logger
Exemplo n.º 8
0
def add_stdout_handler(logger, formatter):
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=(
            'black', 'black', True))
    except ImportError:
        handler = logging.StreamHandler()
        pass

    handler.setFormatter(formatter)
    logger.addHandler(handler)
Exemplo n.º 9
0
def setup_logging(verbose=False):
    log = logging.getLogger("")
    handler = RainbowLoggingHandler(sys.stderr)
    handler._column_color['%(asctime)s'] = ('cyan', None, False)
    handler._column_color['%(levelname)-7s'] = ('green', None, False)
    handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
    handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
    log.addHandler(handler)
    log.setLevel([logging.INFO, logging.DEBUG][verbose])

    logging.getLogger("boto").level = logging.CRITICAL
Exemplo n.º 10
0
    def __init__(self, module_name):
        self.logger_tmp = None
        logger = logging.getLogger(module_name)
        logging.basicConfig(filename='/tmp/r2als.log',level=logging.DEBUG,format='%(asctime)s %(levelname)s %(message)s')
        # logging.basicConfig(level=logging.DEBUG)
        formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default

         # setup `RainbowLoggingHandler`
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)

        logger.addHandler(handler)

        self.logger_tmp = logger
Exemplo n.º 11
0
def test_custom_format():
    logger = logging.getLogger('test_logging_custom_format')
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("MESSAGE ONLY => %(message)s")

    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.debug("debug msg")
    logger.info("info msg")
    logger.warn("warn msg")
    logger.error("error msg")
    logger.critical("critical msg")
Exemplo n.º 12
0
def setup():
    # prepare log file
    global f_log, logpath
    (fd, logpath) = mkstemp(prefix='raibow-', suffix='.txt')
    f_log = os.fdopen(fd, 'w')

    # prepare 'test_format' logger
    logger = logging.getLogger('test_format')
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("AAA %(name)s - %(levelname)s - %(message)s ZZZ")  # testing whether this format is used

    handler = RainbowLoggingHandler(f_log)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
Exemplo n.º 13
0
def get_logger():
    global logger
    
    if logger == None:
        logger = logging.getLogger('freesprints')
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default
        
        # setup colored logging
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    
    return logger
Exemplo n.º 14
0
def setup_logging(verbose=False, silent=False, debug=False):
    log = logging.getLogger("")
    handler = RainbowLoggingHandler(sys.stderr)
    handler._column_color['%(asctime)s'] = ('cyan', None, False)
    handler._column_color['%(levelname)-7s'] = ('green', None, False)
    handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
    handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
    log.addHandler(handler)
    log.setLevel([logging.INFO, logging.DEBUG][verbose or debug])
    if silent:
        log.setLevel(logging.ERROR)

    logging.getLogger("requests").setLevel([logging.CRITICAL, logging.ERROR][verbose or debug])
    return handler
Exemplo n.º 15
0
    def setup_logging(self, args, verbose=False, silent=False, debug=False, logging_name=""):
        """Setup the RainbowLoggingHandler for the logs and call setup_other_logging"""
        log = logging.getLogger(logging_name)
        handler = RainbowLoggingHandler(self.logging_handler_file)
        handler._column_color['%(asctime)s'] = ('cyan', None, False)
        handler._column_color['%(levelname)-7s'] = ('green', None, False)
        handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
        handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
        log.addHandler(handler)
        log.setLevel([logging.INFO, logging.DEBUG][verbose or debug])
        if silent:
            log.setLevel(logging.ERROR)

        self.setup_other_logging(args, verbose, silent, debug)
        return handler
Exemplo n.º 16
0
def setup_TerminalLogger(log_level, logger_name='TerminalLogger'):
    """Setup colorful logger

    *Usage*

    .. code-block:: python
        setup_TerminalLogger(logging.DEBUG)
        logger = logging.getLogger('TerminalLogger')
        logger.debug('hello')
    """
    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)
    handler = RainbowLoggingHandler(sys.stderr, datefmt=None)
    handler.setFormatter(logging.Formatter("[%(asctime)s] %(filename)s %(funcName)s():%(lineno)d\t%(message)s"))
    logger.addHandler(handler)
Exemplo n.º 17
0
def add_stdout_handler(logger):
    formatter = Formatter(
        "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")
    try:
        # Si está instalado rainbow_logging_handler (coloriza el output
        # de consola) lo usamos, sino defaulteamos al módulo logging
        from rainbow_logging_handler import RainbowLoggingHandler
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=(
            'black', 'black', True))
    except ImportError:
        handler = StreamHandler()
        pass

    handler.setFormatter(formatter)
    logger.addHandler(handler)
Exemplo n.º 18
0
def colorlogs():
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        import sys
        # setup `RainbowLoggingHandler`
        logger = logging.root
        formatter = logging.Formatter(
            "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")
        # ^^^ same as default
        handler = RainbowLoggingHandler(
            sys.stderr, color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    except ImportError:
        # rainbow logger not found, that's ok
        pass
Exemplo n.º 19
0
def main_func():

    # Check the number of inputs
    numofinputs = len(sys.argv)

    # Conditional expressions
    if numofinputs > 5 :
        print ("Invalid arguments \nGood BYE")
    else :
        # Get the input arguments
        filename = argv.pop(1) # EXE name, File Name, Module Name
        operation = argv.pop(1) # DEBUG, INFO, WARN, ERROR, CRITICAL, EXCEPTION
        message = argv.pop(1) # Message to be printed
        logFile = argv.pop(1) # Log file

        # setup `logging` module
        logger = logging.getLogger(filename)
        fileHandler = logging.FileHandler('Logger.log')
        logger.setLevel(logging.INFO)
        formatter = logging.Formatter("[%(asctime)s] [%(name)s] : %(message)s")

        # setup `RainbowLoggingHandler`
        handler = RainbowLoggingHandler(sys.stderr, color_name=('grey', None , True),color_asctime=('green', None  , True))
        handler.setFormatter(formatter)
        fileHandler.setFormatter(formatter)
        logger.addHandler(fileHandler)
        logger.addHandler(handler)

        if operation == 'DEBUG' :
            logger.debug(message)
        if operation == 'INFO' :
            logger.info(message)
        if operation == 'WARN' :
            logger.warn(message)
        if operation == 'ERROR' :
            logger.error(message)
        if operation == 'CRITICAL' :
            logger.critical(message)
        if operation == 'EXCEPTION' :
            raise RuntimeError("InstallException!")
            logger.exception(message)
            logger.exception(e)
Exemplo n.º 20
0
def setup_logging(verbose=False, boto_debug=False):
    log = logging.getLogger("")
    handler = RainbowLoggingHandler(sys.stderr)
    handler._column_color['%(asctime)s'] = ('cyan', None, False)
    handler._column_color['%(levelname)-7s'] = ('green', None, False)
    handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
    handler.setFormatter(
        logging.Formatter(
            "%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
    log.addHandler(handler)
    log.setLevel([logging.INFO, logging.DEBUG][verbose])

    if boto_debug:
        logging.getLogger("boto").setLevel(logging.DEBUG)
    else:
        logging.getLogger("boto").setLevel([logging.CRITICAL,
                                            logging.ERROR][verbose])

    logging.getLogger("requests").setLevel([logging.CRITICAL,
                                            logging.ERROR][verbose])
Exemplo n.º 21
0
def colorlogs(format="short"):
    """Append a rainbow logging handler and a formatter to the root logger"""
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        import sys
        # setup `RainbowLoggingHandler`
        logger = logging.root
        # same as default
        if format == "short":
            fmt = "%(message)s "
        else:
            fmt = "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s [%(levelname)s]"
        formatter = logging.Formatter(fmt)
        handler = RainbowLoggingHandler(sys.stderr,
                                        color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    except ImportError:
        # rainbow logger not found, that's ok
        pass
Exemplo n.º 22
0
def setup_console_logging(log_level=None):
    """Setup console logging.

    Aimed to give easy sane defaults for loggig in command line applications.

    Don't use logging settings from INI, but use hardcoded defaults.
    """

    formatter = logging.Formatter("[%(asctime)s] [%(name)s %(funcName)s:%(lineno)d] %(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stdout)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.handlers = [handler]

    env_level = os.environ.get("LOG_LEVEL", "info")
    log_level = log_level or getattr(logging, env_level.upper())
    logger.setLevel(log_level)
def test_usage():
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)

    handler = RainbowLoggingHandler(sys.stderr)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    handler.setFormatter(formatter)
    root_logger.addHandler(handler)
    logger = logging.getLogger('test')

    logger.debug("debug msg")
    logger.info("info msg")
    logger.warn("warn msg")
    logger.error("error msg")
    logger.critical("critical msg")

    try:
        raise RuntimeError("Opa!")
    except Exception as e:
        logger.exception(e)
Exemplo n.º 24
0
 def __init__(self, application_object, plugin_object):
     # setup logger
     self.logger = logging.getLogger('Needles')
     self.logger.setLevel(logging.DEBUG)
     formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default
     
     # setup colored logging
     handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
     handler.setFormatter(formatter)
     self.logger.addHandler(handler)
     
     self.logger.debug("init in Needles plugin")
     self.application = application_object
     self.plugin_object = plugin_object
     self.display_surface = self.application.get_window_surface()
     
     font_path = "./fonts/Cave-Story.ttf"
     distance_font_path = "./fonts/Warenhaus-Standard.ttf"
     self.font_big = pygame.font.Font(distance_font_path, 68)
     self.font_distance = pygame.font.Font(distance_font_path, 50)
Exemplo n.º 25
0
def get_logger(name):
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    handler = logging.FileHandler(os.path.join(config.logs_dir, '{}.log'.format(name)))
    handler.setFormatter(log_formatter)
    handler.setLevel(logging.DEBUG)

    handler2 = RainbowLoggingHandler(
            sys.stdout,
            '%Y-%m-%d %H:%M:%S',
            color_asctime=('white', None, False)
    )
    handler2.setFormatter(log_formatter)
    handler2.setLevel(logging.DEBUG)

    logger.addHandler(handler)
    logger.addHandler(handler2)

    return logger
Exemplo n.º 26
0
def colorlogs(format="short"):
    """Append a rainbow logging handler and a formatter to the root logger"""
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        import sys
        # setup `RainbowLoggingHandler`
        logger = logging.root
        # same as default
        if format == "short":
            fmt = "%(message)s "
        else:
            fmt = "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s [%(levelname)s]"
        formatter = logging.Formatter(fmt)
        handler = RainbowLoggingHandler(sys.stderr,
                                        color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    except ImportError:
        # rainbow logger not found, that's ok
        pass
Exemplo n.º 27
0
def setup_custom_logger():
    LOG_PATH = 'log'
    FileUtils.create_folder_if_not_exists(LOG_PATH)

    LOG_FILE = "{0}/{1}".format(LOG_PATH, "thinklog")

    # logging.Formatter.converter = time.gmtime
    formatter = logging.Formatter(
        "[%(asctime)s] %(threadName)s - %(pathname)s %(funcName)s():%(lineno)d  %(levelname)s \t%(message)s"
    )  # same as default
    # formatter = logging.Formatter('%(asctime)s [%(pathname)s: %(lineno)d] %(levelname)s %(message)s')

    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    file_handler = ParallelTimedRotatingFileHandler(LOG_FILE, when='H')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    # console_handler = logging.StreamHandler(sys.stdout)
    # console_handler.formatter = formatter
    # logger.addHandler(console_handler)

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr,
                                    color_funcName=('black', 'yellow', True))
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    #Add file handler
    handler = TimedRotatingFileHandler(LOG_FILE,
                                       when="h",
                                       interval=1,
                                       backupCount=24)

    # try:
    #     raise RuntimeError("Opa!")
    # except Exception as e:
    #     logger.exception(e)

    return logger
Exemplo n.º 28
0
def enable_logging(filename: str, level: str):
    class Squelch(logging.Filter):
        def filter(self, record):
            if record.levelno == logging.DEBUG:
                return not record.name.startswith('asyncio') and \
                       not record.name.startswith('websockets') and \
                       not record.name.startswith('aiohttp')
            elif record.levelno == logging.INFO and record.name.startswith('asyncio'):
                return False
            return True

    formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)03d:%(levelname)s:%(name)s:%(message)s')

    # File logger captures everything.
    file_handler = logging.FileHandler(filename)
    file_handler.setLevel(logging.DEBUG)

    # Console output level is configurable.
    stream_handler = RainbowLoggingHandler(
        sys.stdout,
        color_asctime=('cyan', None, False),
        color_msecs=('cyan', None, False),
        color_levelname=('gray', None, False),
        color_module=('yellow', None, False),
        color_name=('blue', None, False),
        color_lineno=('green', None, False),
        datefmt="%Y-%m-%d %H:%M:%S"
    )
    stream_handler.setLevel(getattr(logging, level))
    stream_handler.addFilter(Squelch())

    # Set an output format.
    stream_handler.setFormatter(formatter)
    file_handler.setFormatter(formatter)
    file_handler.addFilter(Squelch())

    # Add handlers to root.
    root = logging.getLogger('')
    root.setLevel(logging.DEBUG)
    root.addHandler(stream_handler)
    root.addHandler(file_handler)
Exemplo n.º 29
0
def _setup_logging(verbose, no_color):
    """Set up Python logging based on verbosity level.

    :param verbose: verbosity level
    :param no_color: do not use colorized output
    """
    level = logging.WARNING
    if verbose == 1:
        level = logging.INFO
    elif verbose > 1:
        level = logging.DEBUG

    logger = logging.getLogger()
    logger.setLevel(level)

    if not no_color:
        formatter = logging.Formatter("%(process)d: [%(asctime)s] %(name)s %(funcName)s:%(lineno)d: %(message)s")
        # setup RainbowLoggingHandler
        handler = RainbowLoggingHandler(sys.stderr)
        handler.setFormatter(formatter)
        logger.addHandler(handler)
Exemplo n.º 30
0
class MyLogging(object):
    """
    自定义logging类
    """
    def __init__(self):
        """
        初始化类
        """
        self.formatter = logging.Formatter(
            '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
        )
        self.local_time = time.strftime("%Y%m%d-%H%M%S")
        self.path = 'result\log'
        if not os.path.exists(self.path):
            os.makedirs(self.path)
        # 日志文件
        self.file_handler = logging.FileHandler('result\log\Testlog_%s.log' %
                                                self.local_time)
        # 控制台日志
        self.color_handler = RainbowLoggingHandler(sys.stderr,
                                                   color_funcName=('black',
                                                                   'yellow',
                                                                   True))

    def get_logger(self):
        """
        获取日志实例
        :return: None
        """
        logger = logging.getLogger("Logger")
        logger.setLevel(logging.DEBUG)
        self.file_handler.setLevel(logging.DEBUG)
        self.file_handler.setFormatter(self.formatter)
        self.color_handler.setFormatter(self.formatter)
        logger.addHandler(self.file_handler)
        logger.addHandler(self.color_handler)
        return logger
Exemplo n.º 31
0
def setup():
    formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.addHandler(handler)

    if "VERBOSE_TEST" in os.environ:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.ERROR)

    logger = logging.getLogger("requests.packages.urllib3.connectionpool")
    logger.setLevel(logging.ERROR)

    logger = logging.getLogger("cryptoassets.core.backend.bitcoind")
    logger.setLevel(logging.WARN)

    # SQL Alchemy transactions
    logger = logging.getLogger("txn")
    logger.setLevel(logging.ERROR)
Exemplo n.º 32
0
from rainbow_logging_handler import RainbowLoggingHandler
import datetime

if __name__ == "__main__":
    # logger取得
    logger = logging.getLogger("log")

    # レベル設定
    logger.setLevel(logging.DEBUG)

    # 出力フォーマット
    formatter = logging.Formatter(fmt="%(asctime)s %(message)s")

    # 色付け出力ハンドラの登録
    rainbow_handler = RainbowLoggingHandler(sys.stderr, color_message_info=("green", None, True))
    rainbow_handler.setFormatter(formatter)
    logger.addHandler(rainbow_handler)

    # ファイル出力用ハンドラの登録
    date_obj = datetime.datetime.now()
    date_str = date_obj.strftime("%Y-%m-%d-%H:%M:%S")
    logfilename = "./log/%s.log" % (date_str)
    file_handler = logging.FileHandler(logfilename, "w")
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    logger.debug("this is debug")
    logger.info("this is info")
    logger.warning("this is warning")
    logger.error("this is error")
    logger.critical("this is critical")
Exemplo n.º 33
0
        """
        Creates an ordered dict out of a list of elements.

        :param items: list of elements
        :param key_func: function that returns a key for each passed list element
        :return: created ordered dict with the elements in the same order as in the passed list
        """
        if items is None:
            return InsertionTimeOrderedDict()
        ret = InsertionTimeOrderedDict()
        for item in items:
            ret[key_func(item)] = item
        return ret

#formatter = logging.Formatter("[%(asctime)s] %(name)s %(levelname)s \t%(message)s")
# setup `RainbowLoggingHandler`
handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
""" Colored logging handler that is used for the root logger """
handler.setFormatter(logging.Formatter("[%(asctime)s] %(message)s"))
logging.getLogger().addHandler(handler)


def geom_std(values: t.List[float]) -> float:
    """
    Calculates the geometric standard deviation for the passed values.
    Source: https://en.wikipedia.org/wiki/Geometric_standard_deviation
    """
    import scipy.stats as stats
    import scipy as sp
    gmean = stats.gmean(values)
    return sp.exp(sp.sqrt(sp.sum([sp.log(x / gmean) ** 2 for x in values]) / len(values)))
# set up stream handler
if not disable_stream_handler:
    try:
        # Rainbow Logging
        from rainbow_logging_handler import RainbowLoggingHandler  # noqa: E402
        color_msecs = ('green', None, True)
        stream_handler = RainbowLoggingHandler(
            sys.stdout, color_msecs=color_msecs, datefmt=datefmt
        )
        # msecs color
        stream_handler._column_color['.'] = color_msecs
        stream_handler._column_color['%(asctime)s'] = color_msecs
        stream_handler._column_color['%(msecs)03d'] = color_msecs
    except Exception:
        stream_handler = StreamHandler()

    # the level of output logging
    stream_handler.setLevel(DEBUG)
    stream_handler.setFormatter(default_fmt)
    logger.addHandler(stream_handler)

if not disable_file_handler:
    file_handler = FileHandler(filename=save_filename)

    # the level of output logging
    file_handler.setLevel(DEBUG)
    file_handler.setFormatter(default_fmt)
    logger.addHandler(file_handler)

Exemplo n.º 35
0
#! /usr/bin/env python
# -*- coding:utf-8 -*-

# Standard lib imports
import logging.handlers
import sys
import time
# Third Party imports
from rainbow_logging_handler import RainbowLoggingHandler
# BITSON imports

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

log_format = "".join(["[%(asctime)s] %(levelname)8s: ", "%(message)s"])

formatter = logging.Formatter(fmt=log_format)
# Format UTC Time
formatter.converter = time.gmtime

# Console Handler
ch = RainbowLoggingHandler(sys.stderr)
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
Exemplo n.º 36
0
# Standard lib imports
import logging.handlers
import time
import os
import sys
# Third Party imports
from rainbow_logging_handler import RainbowLoggingHandler
# BITSON imports
from config import Config

console_logger = logging.getLogger('trest')
console_logger.setLevel(logging.DEBUG)

console_handler = RainbowLoggingHandler(sys.stderr)
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(logging.Formatter(fmt="%(message)s"))

console_logger.addHandler(console_handler)

log_format = "".join(
    ["[%(asctime)s] %(name)20s - %(levelname)8s: ",
     "%(threadName)15s-%(funcName)15s() - %(message)s"]
)
formatter = logging.Formatter(fmt=log_format)
# Format UTC Time
formatter.converter = time.gmtime

if not os.path.isdir(Config.LOG_FOLDER):
    os.mkdir(Config.LOG_FOLDER)

logfile = os.path.join(Config.LOG_FOLDER,
Exemplo n.º 37
0
def initialize_logger(args, script_name, syslog_server=None):
    """
    Initialize the logging instance with the values provided
    by the user and return the logging object.

    """
    LOGGER = logging.getLogger(script_name)

    FORMATTER = logging.Formatter(
        ""+script_name+" : "+str(getpass.getuser())+" : %(asctime)s : %(levelname)s : %(message)s",
                                  datefmt='%m/%d/%Y %I:%M:%S %p')

    # create console handler and set level to info
    logging.StreamHandler()

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'white', True))
    handler.setFormatter(FORMATTER)
    LOGGER.addHandler(handler)

    # setup log level from arguments
    if args.log:
        if contains('DEBUG', args.log.upper()):
            handler.setLevel(logging.DEBUG)
            LOGGER.setLevel(logging.DEBUG)
        elif contains('WARNING', args.log.upper()):
            handler.setLevel(logging.WARNING)
            LOGGER.setLevel(logging.WARNING)
        elif contains('ERROR', args.log.upper()):
            handler.setLevel(logging.ERROR)
            LOGGER.setLevel(logging.ERROR)
        else:
            handler.setLevel(logging.INFO)
            LOGGER.setLevel(logging.INFO)
        LOGGER.info('Logging level has been set to {}'.format(args.log.upper()))

    # setup logging directory to store log files
    if args.log_dir:

        if not os.path.isdir(args.log_dir):
            os.makedirs(args.log_dir)
        output_dir = args.log_dir

    else:
        output_dir = create_log_dir()

    LOGGER.info('Logging directory has been set to {}'.format(output_dir))


    # create optional syslog handler, if the argument has been supplied to support it
    if args.syslog:

        DESIG_SYSLOG_SERVER = syslog_server

        handler = logging.handlers.SysLogHandler(address=(DESIG_SYSLOG_SERVER,
                                                          logging.handlers.SYSLOG_UDP_PORT))
        syslog_format = logging.Formatter(
            '[appname]: %(name)s: [alias]: '+str(getpass.getuser())+' %(message)s')
        handler.setFormatter(syslog_format)

        if args.syslog_level:
            # Set syslog level to the user specified level
            if args.syslog_level == 'info':
                handler.setLevel(logging.INFO)
            elif args.syslog_level == 'warning':
                handler.setLevel(logging.WARNING)
            elif args.syslog_level == 'error':
                handler.setLevel(logging.ERROR)

        LOGGER.info(
            'Syslog has been enabled for [{}] logging level, sent to syslog server [{}]'.format(
                                                                            args.syslog_level,
                                                                            DESIG_SYSLOG_SERVER))

        # Add syslog handler to logging object
        LOGGER.addHandler(handler)

    # create error file handler and set level to error
    handler = logging.FileHandler(os.path.join(
        output_dir, "error.log"),"a", encoding=None, delay="true")
    handler.setLevel(logging.ERROR)
    handler.setFormatter(FORMATTER)
    LOGGER.addHandler(handler)

    # create debug file handler and set level to debug
    handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"a")
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(FORMATTER)
    LOGGER.addHandler(handler)

    if args.logfile_rotate:
        '''
        User has specified they would like to create a daily rotating logfile.
        Create an instance and DO NOT create a single serving logfile.
        '''
        LOGGER.info('Logfiles are now set to auto-rotate at midnight UTC.')
        filepath_and_name_format = ('{}/{}'.format(output_dir, script_name.replace('.py','')))
        LOGGER.info('Logfiles are now being written at {}'.format(filepath_and_name_format))

        log_filename=filepath_and_name_format
        # maxBytes takes the max file size in MB and bit-shift converts to bytes
        handler=SizedTimedRotatingFileHandler(
            log_filename, maxBytes=args.logsize<<20)
        handler.setLevel(logging.DEBUG)
        handler.setFormatter(FORMATTER)
        handler.suffix = "%Y-%m-%d.log"
        LOGGER.addHandler(handler)

    else:
        # create individual execution file handler and set level to debug
        now = datetime.datetime.now()
        datetime_stamp = now.strftime("%Y-%m-%d_%H-%M-%S")
        datetime_stamp = ('{}_{}.log'.format(datetime_stamp, script_name.replace('.py','')))
        filename = os.path.join(output_dir, datetime_stamp)

        handler = logging.FileHandler(filename, "a")

        if contains('DEBUG', args.log.upper()):
            handler.setLevel(logging.DEBUG)
        elif contains('WARNING', args.log.upper()):
            handler.setLevel(logging.WARNING)
        elif contains('ERROR', args.log.upper()):
            handler.setLevel(logging.ERROR)
        else:
            handler.setLevel(logging.INFO)

        handler.setFormatter(FORMATTER)
        LOGGER.addHandler(handler)

        # display the exact file name / path to the user
        LOGGER.info('Logs for this session now being written to {}'.format(filename))

        # Attach filepath / filename string to logger
        LOGGER.filename = filename

    return LOGGER
Exemplo n.º 38
0
def main():
    # setup `logging` module
    logger = logging.getLogger('Rancher Deployment')
    # logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr,
        color_funcName=('black', 'yellow', True),
        color_module=('yellow', None, False))

    handler.setFormatter(formatter)
    logger.addHandler(handler)

    # ###################################
    # Importing Args Modules
    # -----------------------------------
    from src import arguments

    # #####################################################
    # 1. Confirming Command Config and Required Arguments
    # -----------------------------------------------------
    # Check to see if arguments have been passed at all
    if arguments.noSystemArgsExist(sys.argv):
        arguments.printHelpDocumentationThenExit()

    # Check if we are printing out the version information
    if arguments.isVersionCommandEntered(sys.argv):
        arguments.printVersionInformationThenExit()

    # Check for the existance of flags
    if arguments.doFlagsExist(sys.argv):
        flags = sys.argv[1]
        arguments.checkHelpFlag(flags)
        FORCE_MODE = arguments.setForceFlag(flags)
        VERBOSE_MODE = arguments.setVerboseFlag(flags)
        DEVELOPMENT_MODE = arguments.setDevelopmentFlag(flags)
    else:
        FORCE_MODE = False
        VERBOSE_MODE = False
        DEVELOPMENT_MODE = False

    if VERBOSE_MODE:
        logger.setLevel(logging.DEBUG)

    logger.info("INFO: Flag Configuration Set")
    logger.debug("DEBUG: Force Mode: %s", FORCE_MODE)
    logger.debug("DEBUG: Verbose Mode: %s", VERBOSE_MODE)
    logger.debug("DEBUG: Development Mode: %s\n", DEVELOPMENT_MODE)

    if not DEVELOPMENT_MODE:
        arguments.checkArgumentStructure(sys.argv)
        ENV_ARGUMENT = arguments.setEnvironment(sys.argv)
        RANCHER_URL = arguments.setRancherUrl(sys.argv)
        RANCHER_ACCESS_KEY = arguments.setRancherKey(sys.argv)
        RANCHER_SECRET_KEY = arguments.setRancherSecret(sys.argv)
    else:
        logger.info("INFO: Currently In Development Mode. Setting Default Parameters.")
        ENV_ARGUMENT = "staging"
        RANCHER_URL = 'http://localhost:8080/v1/'
        RANCHER_ACCESS_KEY = '9F68C78100A2CAA209EC'
        RANCHER_SECRET_KEY = 'pEkMsBYjcZNxhY4rzYuEfdLLj7mDBZ8EPYwbtgVZ'

    if not FORCE_MODE:
        print "Rancher Arguments Set"
        print "ENVIRONMENT: %s" % ENV_ARGUMENT
        logger.debug("DEBUG: RANCHER_URL: %s", RANCHER_URL)
        logger.debug("DEBUG: RANCHER_ACCESS_KEY: %s", RANCHER_ACCESS_KEY)
        logger.debug("DEBUG: RANCHER_SECRET_KEY: %s", RANCHER_SECRET_KEY)
        print "Would you like to continue?"
        var = raw_input("Please enter (Y|N): ")
        if var == "y" or var == "Y":
            print "User Confirmation Accepted. Performing Rancher Deployment"
            logger.debug("DEBUG: Please use the [-f] flag to force application execution and skip confirmation")
        elif var == "n" or var == "N":
            logger.error("ERROR: User stopped app execution.")
            logger.debug("DEBUG: Please use the [-f] flag to force application execution and skip confirmation")
            print sys.exit(0)
        else:
            logger.error("ERROR: Invalid User Input")
            logger.error("ERROR: Please use the [-f] flag to force application execution and skip confirmation")
            print sys.exit(0)
    else:
        logger.info("INFO: Force Mode Enabled. Skipping Flag Confirmation")


    print "Starting Matador Deploy..."
    # ##################################
    # Import Additional Custom Modules
    # ----------------------------------
    # NOTE: This is done here so that the global vars can be used in the inner modules
    from src import yml_reader
    from src import compose_builder
    from src import rancher_compose

    # ##################################
    # 2. Reading YAML Files Into Script
    # ----------------------------------
    rancher_compose_list = yml_reader.readRancherComposeTemplate()
    docker_compose_list = yml_reader.readDockerComposeTemplate()
    config_file = yml_reader.readConfigurationFile()
    global_config = yml_reader.getGlobalConfig()
    env_config = yml_reader.getEnvConfig(ENV_ARGUMENT)
    PROJECT_NAME = config_file['project_name'] + "-" + ENV_ARGUMENT

    # ##################################################
    # 3. Combine config into the rancher compose
    # --------------------------------------------------
    compose_builder.addConfigToDockerCompose(docker_compose_list, global_config)
    compose_builder.addConfigToDockerCompose(docker_compose_list, env_config)

    # ###############################################
    # 4. Set the image for the deployment
    # -----------------------------------------------
    compose_builder.setImageForDockerConfig(docker_compose_list, ENV_ARGUMENT, config_file['image_base'])

    # ###############################################
    # 5. Save new yml out to a temp file
    # -----------------------------------------------
    yml_reader.createBuildDirectory()
    yml_reader.saveRancherComposeFile(rancher_compose_list)
    yml_reader.saveDockerComposeFile(docker_compose_list)

    # ###############################################
    # 6. Start updating this stuff to rancher baby
    # -----------------------------------------------
    cattle_client = cattle.Client(
        url=RANCHER_URL,
        access_key=RANCHER_ACCESS_KEY,
        secret_key=RANCHER_SECRET_KEY
    )
    rancher_compose.setRancherVars(RANCHER_URL, RANCHER_ACCESS_KEY, RANCHER_SECRET_KEY, PROJECT_NAME)
    rancher_compose.checkForExistingEnvironment(cattle_client, PROJECT_NAME)
    rancher_compose.pushToRancher()
Exemplo n.º 39
0
import logging, traceback, sys
from rainbow_logging_handler import RainbowLoggingHandler

# logging.basicConfig(level=logging.DEBUG, format='%(filename)-15s:%(lineno)-4d %(levelname)s: %(message)s')
handler = RainbowLoggingHandler(sys.stderr)
handler.setFormatter(logging.Formatter("%(filename)s:%(lineno)d\t%(levelname)s: %(message)s"))
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)

logging.getLogger("OpenGL").setLevel(99)

# allow importing from the repository root
import sys, os, json

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import apc.baxter_scoop as baxter
from integration.visualization import debug_cloud
from integration.camera import uvtexture, invalid_mask
from integration.io import pcd
from perception.segmentation.color import rgb2yuv, make_uv_hist

from klampt.robotsim import WorldModel
from klampt import se3, so3
from time import sleep, time
import numpy, math
from matplotlib import pyplot


def load_histogram(obj, n):
Exemplo n.º 40
0
import logging
import sys

from rainbow_logging_handler import RainbowLoggingHandler

# setup `logging` module
logger = logging.getLogger('console')
formatter = logging.Formatter("%(message)s")

# setup `RainbowLoggingHandler`
handler = RainbowLoggingHandler(sys.stderr,
                                color_funcName=('black', 'yellow', True))
handler.setFormatter(formatter)
logger.addHandler(handler)


def get_logger():
    return logger


def set_level(level):
    logger.setLevel(level)
Exemplo n.º 41
0
"""
Initialise the application.

@author: Chris Scott

"""
import sys
import platform
import logging
try:
    from rainbow_logging_handler import RainbowLoggingHandler
    root = logging.getLogger()
    root.setLevel(logging.NOTSET)
    formatter = logging.Formatter("%(name)s:%(funcName)s():%(lineno)d: %(message)s")
    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    root.addHandler(handler)
except ImportError:
    # configure logging (we have to set logging.NOTSET here as global for root logger)
    logging.basicConfig(format="%(levelname)s: %(name)s: %(message)s", level=logging.NOTSET)

from PySide import QtGui, QtCore

# set default for stream handler (we don't want it to be NOTSET by default)
_argLevel = None
if len(sys.argv) > 1:
    if "DEBUG" in sys.argv:
        _argLevel = logging.DEBUG
    elif "INFO" in sys.argv:
        _argLevel = logging.INFO
    elif "WARNING" in sys.argv:
Exemplo n.º 42
0
from pensive.coders import register_numpy

logger = logging.getLogger(__name__)
# configure the root logger to accept all records
logger = logging.getLogger()
logger.setLevel(logging.NOTSET)

formatter = logging.Formatter(
    '%(asctime)s\t[%(name)s] %(pathname)s:%(lineno)d\t%(levelname)s:\t%(message)s'
)

# set up colored logging to console
from rainbow_logging_handler import RainbowLoggingHandler

console_handler = RainbowLoggingHandler(sys.stderr)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)


class RealsenseCalibration(QtWidgets.QWidget):
    def __init__(self):
        super(RealsenseCalibration, self).__init__()
        self.initUI()
        self.thread = VideoThread()
        self.thread.signal.connect(self.updateView)
        cameramat, cameracoeff = self.thread.getIntrinsics()
        if cameramat is None:
            logger.warning(
                "Unable to get camera coefficients for camera. Setting to zeros..."
            )
            cameramat = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
Exemplo n.º 43
0
def main(instruments, mode, log_level, debug, step, start_date=None,
         end_date=None):
    """
    Algorithmic trading tool.
    """

    _start_time = time()
    if debug:
        def exception_handler(type, value, traceback):
            click.secho(
                '\nAn exception occurred while executing the requested '
                'command:', fg='red'
            )
            hr(fg='red')
            sys.__excepthook__(type, value, traceback)
            click.secho('\nStarting interactive debugging session:', fg='red')
            hr(fg='red')
            pdb.post_mortem(traceback)

        sys.excepthook = exception_handler

    if step:
        pdb.set_trace()

    # Make urllib3 logger more calm
    urllib3_logger = logging.getLogger('urllib3')
    urllib3_logger.setLevel(logging.CRITICAL)

    try:
        os.makedirs('logs')
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

    numeric_level = getattr(logging, log_level.upper(), None)
    if not isinstance(numeric_level, int):
        raise ValueError('Invalid log level: %s' % log_level)
    log_filename = "logs/pyfx_debug_{}-{}.log".format(
        strftime("%Y_%m_%d-%H_%M"), settings.ENVIRONMENT)
    logging.basicConfig(filename=log_filename, level=logging.DEBUG)

    formatter = logging.Formatter(
        "[%(asctime)s/%(levelname)s] %(funcName)s():%(lineno)d\t%(message)s")
    handler = RainbowLoggingHandler(
        sys.stdout, color_funcName=('black', 'yellow', True))
    handler.setFormatter(formatter)
    handler.setLevel(numeric_level)
    log.addHandler(handler)

    start_date_ = start_date if start_date else settings.BACKTEST_START
    end_date_ = end_date if end_date else settings.BACKTEST_END
    BACKTEST_START = parser.parse(start_date_).replace(tzinfo=pytz.utc)
    BACKTEST_END = parser.parse(end_date_).replace(tzinfo=pytz.utc)

    if mode == 'backtest':
        api = oandapy.API(
            environment=settings.ENVIRONMENT,
            access_token=settings.ACCESS_TOKEN,
        )
        broker = OandaBacktestBroker(
            api=api,
            account_id=settings.ACCOUNT_ID,
            initial_balance=decimal.Decimal(5000))

        # Oanda 20:00, Local: 22:00, DailyFx: 16:00
        clock_interval = settings.CLOCK_INTERVAL
        log.info('Starting simulated clock with interval {} seconds'.format(clock_interval))
        clock = SimulatedClock(
            start=BACKTEST_START,
            stop=BACKTEST_END,
            interval=clock_interval,
        )

    elif mode == 'live':
        api = oandapy.API(
            environment=settings.ENVIRONMENT,
            access_token=settings.ACCESS_TOKEN,
        )
        clock = IntervalClock(interval=settings.CLOCK_INTERVAL)
        broker = OandaRealtimeBroker(api=api, account_id=settings.ACCOUNT_ID)
    else:
        raise NotImplementedError()

    # TODO Optimize load of instrument info
    instrument_list = set(instruments)
    for inst in instrument_list:
        inst.load(broker)
    # TODO We have to be able to instantiate strategies with custom args
    strategies = [settings.STRATEGY(instrument)
                  for instrument in instrument_list]
    if mode == 'backtest':
        broker.init_backtest(BACKTEST_START, BACKTEST_END, strategies)
        pf = Portfolio(broker, mode='backtest')
    else:
        pf = Portfolio(broker, mode='live')
    controller = Controller(clock, broker, pf, strategies)
    controller.run_until_stopped()

    log.info('script duration: {:.2f}s'.format(time() - _start_time))
Exemplo n.º 44
0
# set up handler
try:
    # Rainbow Logging
    import sys
    from rainbow_logging_handler import RainbowLoggingHandler
    color_msecs = ('black', None, True)
    default_handler = RainbowLoggingHandler(sys.stdout,
                                            color_msecs=color_msecs,
                                            datefmt=datefmt)
    # HACK for msecs color
    default_handler._column_color['.'] = color_msecs
    default_handler._column_color['%(msecs)03d'] = color_msecs
except Exception:
    default_handler = logging.StreamHandler()

default_handler.setFormatter(default_fmt)
default_handler.setLevel(logging.DEBUG)

# setup root logger
logger = logging.getLogger()
logger.addHandler(default_handler)
logger.setLevel(logging.DEBUG)


def set_fmt(fmt=default_fmt):
    global defaut_handler
    default_handler.setFormatter(fmt)


def set_root_level(level):
    global logger
Exemplo n.º 45
0
logging.basicConfig(level=logging.DEBUG,
                    format='[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='spam.log',
                    filemode='w')

# define a Handler which writes INFO messages or higher to the sys.stderr
console = RainbowLoggingHandler(sys.stdout) #logging.StreamHandler() 
# handler = RainbowLoggingHandler(sys.stdout)

# get level from command input
console.setLevel(log_level)

formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
logger = logging.getLogger('nrk2013')
# tell requests lib to chill out
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARNING)


# Dette skulle hjelpe?
# En ekte glob!
request = False

### Hjelpefunksjoner

def dispatch_on_template(soup, data, dictionary):
Exemplo n.º 46
0
def main(instruments, mode, log_level, start_date=None, end_date=None):
    """
    Algorithmic trading tool.
    """

    # Make urllib3 logger more calm
    urllib3_logger = logging.getLogger('urllib3')
    urllib3_logger.setLevel(logging.CRITICAL)

    numeric_level = getattr(logging, log_level.upper(), None)
    if not isinstance(numeric_level, int):
        raise ValueError('Invalid log level: %s' % log_level)
    log_filename = "logs/pyfx_debug_{}-{}.log".format(strftime("%Y_%m_%d-%H_%M"), settings.ENVIRONMENT)
    logging.basicConfig(filename=log_filename, level=logging.DEBUG)
    logger = logging.getLogger('pyFx')

    formatter = logging.Formatter("[%(asctime)s/%(levelname)s] %(funcName)s():%(lineno)d\t%(message)s")
    handler = RainbowLoggingHandler(sys.stdout, color_funcName=('black', 'yellow', True))
    handler.setFormatter(formatter)
    handler.setLevel(numeric_level)
    logger.addHandler(handler)

    start_date_ = start_date if start_date else settings.BACKTEST_START
    end_date_ = end_date if end_date else settings.BACKTEST_END
    BACKTEST_START = parser.parse(start_date_).replace(tzinfo=pytz.utc)
    BACKTEST_END = parser.parse(end_date_).replace(tzinfo=pytz.utc)

    if mode == 'backtest':
        api = oandapy.API(
            environment=settings.ENVIRONMENT,
            access_token=settings.ACCESS_TOKEN,
        )
        broker = OandaBacktestBroker(
            api=api,
            account_id=settings.ACCOUNT_ID,
            initial_balance=decimal.Decimal(5000))

        # Oanda 20:00, Local: 22:00, DailyFx: 16:00
        clock = SimulatedClock(
            start=BACKTEST_START,
            stop=BACKTEST_END,
            interval=settings.CLOCK_INTERVAL,
        )

    elif mode == 'live':
        api = oandapy.API(
            environment=settings.ENVIRONMENT,
            access_token=settings.ACCESS_TOKEN,
        )
        clock = IntervalClock(interval=settings.CLOCK_INTERVAL)
        broker = OandaRealtimeBroker(api=api, account_id=settings.ACCOUNT_ID)
    else:
        raise NotImplementedError()

    # TODO Optimize load of instrument info
    instrument_list = set(instruments)
    for inst in instrument_list:
        inst.load(broker)
    # TODO We have to be able to instantiate strategies with custom args
    strategies = [settings.STRATEGY(instrument)
                  for instrument in instrument_list]
    if mode == 'backtest':
        broker.init_backtest(BACKTEST_START, BACKTEST_END, strategies)
        pf = Portfolio(broker, mode='backtest')
    else:
        pf = Portfolio(broker, mode='live')
    controller = Controller(clock, broker, pf, strategies)
    controller.run_until_stopped()
Exemplo n.º 47
0
class MultiProcessingLog(logging.Handler):
    def __init__(self, name, mode, maxsize, rotate):
        logging.Handler.__init__(self)

        #self._handler = RotatingFileHandler(name, mode, maxsize, rotate)
        formatter = logging.Formatter(
            "[%(asctime)s.%(msecs)03d] %(filename)16s Line %(lineno)3d %(funcName)s():\t %(message)s"
        )
        self._handler = RainbowLoggingHandler(sys.stderr,
                                              color_funcName=('green', 'none',
                                                              True))
        self._handler.setFormatter(formatter)

        self.queue = multiprocessing.Queue(-1)

        t = threading.Thread(target=self.receive)
        t.daemon = True
        t.start()

    def setFormatter(self, fmt):
        logging.Handler.setFormatter(self, fmt)
        self._handler.setFormatter(fmt)

    def receive(self):
        while True:
            try:
                record = self.queue.get()
                self._handler.emit(record)
            except (KeyboardInterrupt, SystemExit):
                raise
            except EOFError:
                break
            except:
                traceback.print_exc(file=sys.stderr)

    def send(self, s):
        self.queue.put_nowait(s)

    def _format_record(self, record):
        # ensure that exc_info and args
        # have been stringified.  Removes any chance of
        # unpickleable things inside and possibly reduces
        # message size sent over the pipe
        if record.args:
            record.msg = record.msg % record.args
            record.args = None
        if record.exc_info:
            dummy = self.format(record)
            record.exc_info = None

        return record

    def emit(self, record):
        try:
            s = self._format_record(record)
            self.send(s)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)

    def close(self):
        self._handler.close()
        logging.Handler.close(self)