本文整理汇总了Python中structlog.configure函数的典型用法代码示例。如果您正苦于以下问题:Python configure函数的具体用法?Python configure怎么用?Python configure使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了configure函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: configure_logging
def configure_logging(app):
if app.config.get('JSON_STRUCTURED_LOGGING'):
processors = [
structlog.stdlib.filter_by_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
mozdef_format,
structlog.processors.JSONRenderer()
]
else:
processors = [
structlog.stdlib.filter_by_level,
structlog.stdlib.PositionalArgumentsFormatter(),
UnstructuredRenderer()
]
if app.config.get('JSON_STRUCTURED_LOGGING') and stdout_log:
# structlog has combined all of the interesting data into the
# (JSON-formatted) message, so only log that
stdout_log.setFormatter(logging.Formatter('%(message)s'))
structlog.configure(
context_class=structlog.threadlocal.wrap_dict(dict),
processors=processors,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
开发者ID:Callek,项目名称:build-relengapi,代码行数:29,代码来源:logging.py
示例2: setup_logging
def setup_logging(config):
"""Setup structured logging, and emit `request.summary` event on each
request, as recommanded by Mozilla Services standard:
* https://mana.mozilla.org/wiki/display/CLOUDSERVICES/Logging+Standard
* http://12factor.net/logs
"""
settings = config.get_settings()
renderer_klass = config.maybe_dotted(settings['cliquet.logging_renderer'])
renderer = renderer_klass(settings)
structlog.configure(
# Share the logger context by thread.
context_class=structlog.threadlocal.wrap_dict(dict),
# Integrate with Pyramid logging facilities.
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
# Setup logger output format.
processors=[
structlog.stdlib.filter_by_level,
structlog.processors.format_exc_info,
renderer,
])
def on_new_request(event):
request = event.request
# Save the time the request was received by the server.
event.request._received_at = utils.msec_time()
# New logger context, with infos for request summary logger.
logger.new(agent=request.headers.get('User-Agent'),
path=event.request.path,
method=request.method,
querystring=dict(request.GET),
uid=request.authenticated_userid,
lang=request.headers.get('Accept-Language'),
auth_type=getattr(request, 'auth_type', None),
errno=None)
config.add_subscriber(on_new_request, NewRequest)
def on_new_response(event):
response = event.response
request = event.request
# Compute the request processing time in msec (-1 if unknown)
current = utils.msec_time()
duration = current - getattr(request, '_received_at', current - 1)
isotimestamp = datetime.fromtimestamp(current/1000).isoformat()
# Bind infos for request summary logger.
logger.bind(time=isotimestamp,
code=response.status_code,
t=duration)
# Ouput application request summary.
logger.info('request.summary')
config.add_subscriber(on_new_response, NewResponse)
开发者ID:jotes,项目名称:cliquet,代码行数:60,代码来源:logs.py
示例3: get_tiger
def get_tiger():
"""
Sets up logging and returns a new tasktiger instance.
"""
structlog.configure(
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
)
logging.basicConfig(format='%(message)s')
conn = redis.Redis(db=TEST_DB)
tiger = TaskTiger(connection=conn, config={
# We need this 0 here so we don't pick up scheduled tasks when
# doing a single worker run.
'SELECT_TIMEOUT': 0,
'LOCK_RETRY': DELAY*2.,
'DEFAULT_RETRY_METHOD': fixed(DELAY, 2),
'BATCH_QUEUES': {
'batch': 3,
}
})
tiger.log.setLevel(logging.CRITICAL)
return tiger
开发者ID:flow180,项目名称:tasktiger,代码行数:25,代码来源:utils.py
示例4: get_logger
def get_logger(level=logging.DEBUG, name=None, stream=DEFAULT_STREAM):
"""Configure and return a logger with structlog and stdlib."""
wrap_dict_class = structlog.threadlocal.wrap_dict(dict)
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_log_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt='iso'),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.JSONRenderer(sort_keys=True)
],
context_class=wrap_dict_class,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True)
log = structlog.get_logger(name)
if not _has_streamhandler(logging.getLogger(name),
level=level, stream=stream):
streamhandler = logging.StreamHandler(stream)
streamhandler.setLevel(level)
streamhandler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))
log.addHandler(streamhandler)
log.setLevel(level)
return log
开发者ID:Gifflen,项目名称:fleece,代码行数:27,代码来源:log.py
示例5: configure_logging
def configure_logging(log_format, utc, endpoint):
processors = [
TimeStamper(
key='@timestamp',
utc=utc,
),
]
if endpoint.startswith('file://'):
path = endpoint[7:]
if path == '/dev/stdout':
stream = sys.stdout
elif path == '/dev/stderr':
stream = sys.stderr
else:
stream = open(path, 'w')
logger_factory = structlog.PrintLoggerFactory(file=stream)
if log_format == 'kv':
processors.append(structlog.processors.KeyValueRenderer(
sort_keys=True,
key_order=['@timestamp', 'event'],
))
else:
processors.append(structlog.processors.JSONRenderer(
sort_keys=True,
))
elif endpoint.startswith('fluent://'):
utc = True
logger_factory = FluentLoggerFactory.from_url(endpoint)
else:
raise ValueError('Invalid logging endpoint "%s".' % endpoint)
structlog.configure(
processors=processors,
logger_factory=logger_factory,
)
开发者ID:smartmob-project,项目名称:smartmob-agent,代码行数:34,代码来源:__init__.py
示例6: test_format_event_basic
def test_format_event_basic(self):
processors = [teeth_overlord.service._format_event,
_return_event_processor]
structlog.configure(processors=processors)
log = structlog.wrap_logger(structlog.ReturnLogger())
logged_msg = log.msg("hello {word}", word='world')
self.assertEqual(logged_msg, "hello world")
开发者ID:rackerlabs,项目名称:teeth-overlord,代码行数:7,代码来源:service.py
示例7: global_setup
def global_setup(config):
"""Perform global cofiguration. In a given process, this should only
ever be called with a single configuration instance. Doing otherwise
will result in a runtime exception.
"""
global _global_config
if _global_config is None:
_global_config = config
# this breaks with unicode :(
connection.setup([str(v) for v in config.CASSANDRA_CLUSTER],
consistency=config.CASSANDRA_CONSISTENCY)
processors = [
_capture_stack_trace,
_format_event,
]
if config.PRETTY_LOGGING:
processors.append(structlog.processors.ExceptionPrettyPrinter())
processors.append(structlog.processors.KeyValueRenderer())
else:
processors.append(structlog.processors.JSONRenderer())
structlog.configure(
processors=processors
)
elif _global_config != config:
raise Exception('global_setup called twice with different '
'configurations')
开发者ID:rackerlabs,项目名称:teeth-overlord,代码行数:29,代码来源:service.py
示例8: run
def run():
logging.basicConfig(format=settings.LOGGING_FORMAT,
datefmt="%Y-%m-%dT%H:%M:%S",
level=settings.LOGGING_LEVEL)
logging.getLogger('sdc.rabbit').setLevel(logging.INFO)
# These structlog settings allow bound fields to persist between classes
structlog.configure(logger_factory=LoggerFactory(), context_class=wrap_dict(dict))
logger = structlog.getLogger()
logger.info('Starting SDX Downstream', version=__version__)
message_processor = MessageProcessor()
quarantine_publisher = QueuePublisher(
urls=settings.RABBIT_URLS,
queue=settings.RABBIT_QUARANTINE_QUEUE
)
message_consumer = MessageConsumer(
durable_queue=True,
exchange=settings.RABBIT_EXCHANGE,
exchange_type='topic',
rabbit_queue=settings.RABBIT_QUEUE,
rabbit_urls=settings.RABBIT_URLS,
quarantine_publisher=quarantine_publisher,
process=message_processor.process
)
try:
message_consumer.run()
except KeyboardInterrupt:
message_consumer.stop()
开发者ID:ONSdigital,项目名称:sdx-downstream,代码行数:34,代码来源:main.py
示例9: configure_logging
def configure_logging(config):
structlog.configure(processors=processors,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True)
config.setdefault('logging', {})
config['logging'].setdefault('version', 1)
config['logging'].setdefault('handlers', {})
config['logging'].setdefault('formatters', {})
config['logging'].setdefault('loggers', {})
config['logging']['handlers'].setdefault('raw', {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'raw',
})
config['logging']['loggers'].setdefault('root', {
'handlers': ['raw'],
'level': 'DEBUG',
'propagate': False,
})
config['logging']['loggers'].setdefault('graphite_api', {
'handlers': ['raw'],
'level': 'DEBUG',
})
config['logging']['formatters']['raw'] = {'()': StructlogFormatter}
dictConfig(config['logging'])
if 'path' in config:
logger.info("loading configuration", path=config['path'])
else:
logger.info("loading default configuration")
开发者ID:Dieterbe,项目名称:graphite-api,代码行数:30,代码来源:config.py
示例10: configure_logging
def configure_logging(logging_levels, plain=False):
_remove_all_existing_log_handlers()
renderer = (
PlainRenderer() if plain else
structlog.processors.JSONRenderer())
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt='iso'),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
renderer
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
handler = logging.StreamHandler(sys.stdout)
root_logger = logging.getLogger()
root_logger.addHandler(handler)
for logger, level in logging_levels.items():
if logger.lower() == 'root':
logger = ''
logging.getLogger(logger).setLevel(level.upper())
开发者ID:xze,项目名称:OpenTAXII,代码行数:35,代码来源:utils.py
示例11: configure
def configure(config_string='', log_json=False):
# configure structlog
processors = [
log_listeners, # before level filtering
structlog.stdlib.filter_by_level,
structlog.processors.StackInfoRenderer()
]
if log_json:
processors.append(JSONRenderer(sort_keys=True))
else:
processors.extend([
structlog.processors.ExceptionPrettyPrinter(file=None),
KeyValueRenderer(sort_keys=True, key_order=None)
])
structlog.configure(
processors=processors,
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=BoundLoggerTrace,
# later calls on configure() dont have any effect on already cached loggers
cache_logger_on_first_use=True,
)
# configure standard logging
if log_json:
format = JSON_FORMAT
else:
format = PRINT_FORMAT
setup_stdlib_logging(DEFAULT_LOGLEVEL, format)
if config_string:
configure_loglevels(config_string)
开发者ID:Feretrius,项目名称:pyethereum,代码行数:30,代码来源:slogging.py
示例12: get_tiger
def get_tiger():
"""
Sets up logging and returns a new tasktiger instance.
"""
structlog.configure(
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
)
logging.basicConfig(format='%(message)s')
conn = redis.Redis(db=TEST_DB, decode_responses=True)
tiger = TaskTiger(connection=conn, config={
# We need this 0 here so we don't pick up scheduled tasks when
# doing a single worker run.
'SELECT_TIMEOUT': 0,
'ACTIVE_TASK_UPDATE_TIMEOUT': 2 * DELAY,
'REQUEUE_EXPIRED_TASKS_INTERVAL': DELAY,
'LOCK_RETRY': DELAY * 2.,
'DEFAULT_RETRY_METHOD': fixed(DELAY, 2),
'BATCH_QUEUES': {
'batch': 3,
},
'SINGLE_WORKER_QUEUES': ['swq'],
})
tiger.log.setLevel(logging.CRITICAL)
return tiger
开发者ID:closeio,项目名称:tasktiger,代码行数:31,代码来源:utils.py
示例13: update_logging
def update_logging(instance_id, vcore_id):
"""
Add the vcore id to the structured logger
:param vcore_id: The assigned vcore id
:return: structure logger
"""
def add_exc_info_flag_for_exception(_, name, event_dict):
if name == 'exception':
event_dict['exc_info'] = True
return event_dict
def add_instance_id(_, __, event_dict):
event_dict['instance_id'] = instance_id
return event_dict
def add_vcore_id(_, __, event_dict):
event_dict['vcore_id'] = vcore_id
return event_dict
processors = [
add_exc_info_flag_for_exception,
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
add_instance_id,
add_vcore_id,
FluentRenderer(),
]
structlog.configure(processors=processors)
# Mark first line of log
log = structlog.get_logger()
log.info("updated-logger")
return log
开发者ID:gcgirish-radisys,项目名称:voltha,代码行数:33,代码来源:structlog_setup.py
示例14: init_logger
def init_logger(project_name,
channel=None,
level=logbook.INFO,
handler=None,
PAPERTRAIL_HOST=None,
PAPERTRAIL_PORT=None,
SENTRY_DSN=None,
MOZDEF=None,
flask_app=None,
timestamp=False,
):
if not channel:
channel = os.environ.get('APP_CHANNEL')
if channel and channel not in CHANNELS:
raise Exception('Initilizing logging with channel `{}`. It should be one of: {}'.format(channel, ', '.join(CHANNELS)))
# By default output logs on stderr
if handler is None:
fmt = '{record.channel}: {record.message}'
handler = logbook.StderrHandler(level=level, format_string=fmt)
handler.push_application()
# Log to papertrail
if channel and PAPERTRAIL_HOST and PAPERTRAIL_PORT:
setup_papertrail(project_name, channel, PAPERTRAIL_HOST, PAPERTRAIL_PORT)
# Log to sentry
if channel and SENTRY_DSN:
setup_sentry(project_name, channel, SENTRY_DSN, flask_app)
def logbook_factory(*args, **kwargs):
# Logger given to structlog
logbook.compat.redirect_logging()
return logbook.Logger(level=level, *args, **kwargs)
# Setup structlog over logbook
processors = [
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
]
if timestamp is True:
processors.append(structlog.processors.TimeStamper(fmt='%Y-%m-%d %H:%M:%S'))
# send to mozdef before formatting into a string
if channel and MOZDEF:
processors.append(setup_mozdef(project_name, channel, MOZDEF))
processors.append(UnstructuredRenderer())
structlog.configure(
context_class=structlog.threadlocal.wrap_dict(dict),
processors=processors,
logger_factory=logbook_factory,
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
开发者ID:La0,项目名称:mozilla-relengapi,代码行数:60,代码来源:log.py
示例15: run
def run():
structlog.configure(
processors=[
structlog.processors.StackInfoRenderer(),
structlog.twisted.JSONRenderer()
],
context_class=dict,
logger_factory=structlog.twisted.LoggerFactory(),
wrapper_class=structlog.twisted.BoundLogger,
cache_logger_on_first_use=True,
)
# grab all of the events that are dispatched to stdlib logger
# new relic uses this.
handler = logging.StreamHandler(sys.stdout)
root_logger = logging.getLogger()
root_logger.addHandler(handler)
# start the twisted logger
twLog.startLogging(sys.stdout)
# api is the WSGI resource returned by Falcon.
api = falcon.API()
api.add_route('/quote', QuoteResource())
app = newrelic.agent.WSGIApplicationWrapper(api)
resource = WSGIResource(reactor, reactor.getThreadPool(), app)
site = Site(resource)
reactor.listenTCP(port=8713, factory=site)
reactor.run()
开发者ID:derwolfe,项目名称:twisted-wsgi-nr,代码行数:29,代码来源:twisted_wsgi_nr.py
示例16: test_structlog_processor
def test_structlog_processor(self):
try:
# Use ReturnLogger for testing
structlog.configure(
processors=[tasktiger_processor],
context_class=dict,
logger_factory=structlog.ReturnLoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
# Run a simple task. Logging output is verified in
# the task.
self.tiger.delay(logging_task)
queues = self._ensure_queues(queued={"default": 1})
task = queues["queued"]["default"][0]
assert task["func"] == "tests.test_logging:logging_task"
Worker(self.tiger).run(once=True)
self._ensure_queues(queued={"default": 0})
assert not self.conn.exists("t:task:%s" % task["id"])
finally:
structlog.configure(
processors=[
structlog.stdlib.add_log_level,
structlog.stdlib.filter_by_level,
structlog.processors.TimeStamper(fmt="iso", utc=True),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.JSONRenderer(),
],
context_class=dict,
logger_factory=structlog.ReturnLoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
开发者ID:closeio,项目名称:tasktiger,代码行数:35,代码来源:test_logging.py
示例17: run
def run():
args = ap.parse_args()
if args.ls:
print('\n'.join(parser.listPluginNames()))
sys.exit(0)
if args.verbose:
structlog.configure(logger_factory=structlog.PrintLoggerFactory(sys.stderr))
infile = BytesIO(sys.stdin.read())
try:
parsed = parse(infile, exclude=args.exclude)
except NoWillingParsers:
if args.strict:
raise
else:
infile.seek(0)
sys.stdout.write(infile.read())
sys.exit(0)
if args.format == 'yaml':
print(yaml.safe_dump(parsed, default_flow_style=False))
elif args.format == 'json':
print(json.dumps(parsed))
elif args.format == 'grep':
from ppo.output import giganticGrep
giganticGrep(parsed, sys.stdout)
开发者ID:iffy,项目名称:ppo,代码行数:28,代码来源:runner.py
示例18: init_app
def init_app(app):
# Output logs on stderr
fmt = '{record.channel}: {record.message}'
stderr = logbook.StderrHandler(format_string=fmt)
stderr.push_application()
def logbook_factory(*args, **kwargs):
# Logger given to structlog
level = app.debug and logbook.DEBUG or logbook.INFO
return logbook.Logger(level=level, *args, **kwargs)
# Setup structlog over logbook
processors = [
# structlog.stdlib.filter_by_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.StackInfoRenderer(),
# structlog.processors.format_exc_info,
]
# send to mozdef before formatting into a string
mozdef = app.config.get('MOZDEF_TARGET', None)
if mozdef:
processors.append(mozdef)
processors.append(UnstructuredRenderer())
structlog.configure(
context_class=structlog.threadlocal.wrap_dict(dict),
processors=processors,
logger_factory=logbook_factory,
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
开发者ID:garbas,项目名称:mozilla-releng,代码行数:34,代码来源:log.py
示例19: configure_structlog
def configure_structlog():
"""
Make structlog comply with all of our options.
"""
from django.conf import settings
import logging
import structlog
from sentry import options
from sentry.logging import LoggingFormat
WrappedDictClass = structlog.threadlocal.wrap_dict(dict)
kwargs = {
'context_class': WrappedDictClass,
'wrapper_class': structlog.stdlib.BoundLogger,
'cache_logger_on_first_use': True,
'processors': [
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.format_exc_info,
structlog.processors.StackInfoRenderer(),
]
}
fmt_from_env = os.environ.get('SENTRY_LOG_FORMAT')
if fmt_from_env:
settings.SENTRY_OPTIONS['system.logging-format'] = fmt_from_env.lower()
fmt = options.get('system.logging-format')
if fmt == LoggingFormat.HUMAN:
from sentry.logging.handlers import HumanRenderer
kwargs['processors'].extend([
structlog.processors.ExceptionPrettyPrinter(),
HumanRenderer(),
])
elif fmt == LoggingFormat.MACHINE:
from sentry.logging.handlers import JSONRenderer
kwargs['processors'].append(JSONRenderer())
structlog.configure(**kwargs)
lvl = os.environ.get('SENTRY_LOG_LEVEL')
if lvl and lvl not in logging._levelNames:
raise AttributeError('%s is not a valid logging level.' % lvl)
settings.LOGGING['root'].update({
'level': lvl or settings.LOGGING['default_level']
})
if lvl:
for logger in settings.LOGGING['overridable']:
try:
settings.LOGGING['loggers'][logger].update({
'level': lvl
})
except KeyError:
raise KeyError('%s is not a defined logger.' % logger)
logging.config.dictConfig(settings.LOGGING)
开发者ID:faulkner,项目名称:sentry,代码行数:59,代码来源:initializer.py
示例20: init_logging
def init_logging(log_dir, name):
if not os.path.exists(log_dir):
os.mkdir(log_dir)
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'plain': {
'()': ProcessorFormatter,
'processor': structlog.dev.ConsoleRenderer(), # TODO: ConsoleRenderer without coloring
},
'colored': {
'()': ProcessorFormatter,
'processor': structlog.dev.ConsoleRenderer(),
},
},
'handlers': {
'default': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'colored',
},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.WatchedFileHandler',
'filename': os.path.join(log_dir, '{}.log'.format(name)),
'formatter': 'plain',
},
},
'loggers': {
'': {
'handlers': ['default', 'file'],
'level': 'DEBUG',
'propagate': True,
},
'asyncio': {
'propagate': False,
},
'telnetlib3': {
'propagate': False,
},
}
})
structlog.configure(
processors=[
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt='%Y-%m-%d %H:%M:%S'),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
event_dict_to_message,
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
sys.excepthook = uncaught_exception
开发者ID:insolite,项目名称:mergeit,代码行数:58,代码来源:common.py
注:本文中的structlog.configure函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论