Remaniement du logging des messages de l'application : une seule info par console...
[minwii.git] / src / app / log.py
1 # -*- coding: utf-8 -*-
2 """
3 utilitaires pour tracer l'activité de miiwii
4
5 $Id$
6 $URL$
7 """
8
9 from events import TIMEOUT, COLOVER, eventNames
10 from event_pprinters import pprinters
11 import sys
12 import pygame
13 import logging
14
15 hiddenEvents = {pygame.MOUSEMOTION:True,
16 pygame.MOUSEBUTTONDOWN:True,
17 pygame.MOUSEBUTTONUP:True,
18 TIMEOUT:True,
19 #COLOVER:True,
20 }
21
22
23 class EventLoggerAdapter(logging.LoggerAdapter) :
24
25 FORMAT = "EVT %(ticks)-10s %(eventName)-15s %(message)s"
26
27 def process(self, msg, kwargs) :
28 pp = pprinters.get(msg.type, lambda e: str(e))
29 extra = {}
30 extra['eventType'] = msg.type
31 extra['eventName'] = eventNames[msg.type]
32 extra['ticks'] = pygame.time.get_ticks()
33 kwargs['extra']=extra
34 return pp(msg), kwargs
35
36 class EventFilter :
37
38 def __init__(self):
39 pass
40
41 def filter(self, record) :
42 return not hiddenEvents.has_key(record.eventType)
43
44 #logging.basicConfig(level=logging.DEBUG, stream = sys.stdout)
45 console = logging.getLogger('minwii.console')
46 console.setLevel(logging.DEBUG)
47 handler = logging.StreamHandler()
48 handler.setFormatter(logging.Formatter('APP %(message)s'))
49 console.addHandler(handler)
50
51 _eventLogger = logging.getLogger('minwii.events')
52 _eventLogger.setLevel(logging.INFO)
53 _eventHandler = logging.StreamHandler()
54 _eventFormatter = logging.Formatter(EventLoggerAdapter.FORMAT)
55 _eventHandler.setFormatter(_eventFormatter)
56 _eventLogger.addHandler(_eventHandler)
57 _eventLogger.addFilter(EventFilter())
58
59 eventLogger = EventLoggerAdapter(_eventLogger, {})
60 #eventLogger = logging.getLogger('minwii.events')