getlogbook / logbook

A cool logging replacement for Python.
http://logbook.readthedocs.org
Other
1.48k stars 165 forks source link

How should I use TaggingHandler? #273

Closed zxfzqf closed 5 years ago

zxfzqf commented 6 years ago

OS:window 10 python version:3.6

I want different levels of logging to be saved to different files. I use TaggingHandler, but it not work, I was wrong?What do I need to do?

import os
import time
import datetime
import logbook
from multiprocessing import Pool
from logbook import Logger, FileHandler
from logbook.more import TaggingHandler

handler_tag = TaggingHandler(dict(
    info=logbook.StderrHandler(level=logbook.INFO, format_string='{record.time:%Y-%m-%d %H:%M:%S}|{record.level_name}|{record.message}'),
    warning=logbook.FileHandler('two_file_log.log', mode='a')
))

handler_tag.push_application()
logger = Logger()

def process_log(p_off):

    for _ in range(0, 100):
        d = datetime.datetime.now()
        t_str = 'testtest' * 2000
        logger.info('{}:{}:p_off({}):pid({})'.format(t_str, d.strftime('%Y-%m-%d %H:%M:%S'), p_off, os.getpid()))

if __name__ == '__main__':

    logger.info('start.....')
    p = Pool(8)
    for i in range(4):
        p.apply_async(process_log, args=(i,))
    print('waiting.....')
    p.close()
    p.join()
    print('done.....')
zxfzqf commented 6 years ago

I try use MultiProcessingHandler ,but it lost data, Is it wrong for me to use the method?

import os
import time
import datetime
import logbook
from multiprocessing import Pool
from multiprocessing import Queue
from logbook import Logger, FileHandler, TimedRotatingFileHandler
from logbook.queues import MultiProcessingHandler, MultiProcessingSubscriber

queue = Queue(-1)
handler_tag = MultiProcessingHandler(queue)
handler_tag.push_application()
logger = Logger()

target_handlers = logbook.NestedSetup([
    logbook.NullHandler(),
    logbook.FileHandler('file_log.log', mode='a', level=logbook.DEBUG)
])

sub = MultiProcessingSubscriber(queue)
c = sub.dispatch_in_background(target_handlers)

def process_log(p_off):
    for x in range(0, 100):
        d = datetime.datetime.now()
        t_str = 'testtest%d'%x
        logger.warning('{}:{}:p_off({}):pid({})'.format(t_str, d.strftime('%Y-%m-%d %H:%M:%S'), p_off, os.getpid()))

if __name__ == '__main__':

    logger.info('start.....')
    p = Pool(4)
    for i in range(4):
        p.apply_async(process_log, args=(i,))
    print('waiting.....')
    p.close()
    p.join()
    print('done.....')
zxfzqf commented 6 years ago

use MultiProcessingHandler code at linux is work .win10 …… Can anyone solve my problem?

zxfzqf commented 5 years ago

TaggingHandler ....,It still doesn't work.. I didn't find an example of work.
no one responded to me. depressed…… call it quits