Package pyjamas :: Package logging :: Module handlers
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.logging.handlers

  1  """Logging handlers for Pyjamas logging based on CPython's logging handlers.""" 
  2  __author__ = 'Peter Bittner <peter.bittner@gmx.net>' 
  3   
  4  from cgi import escape 
  5  from logging import Handler 
  6  from pyjamas import DOM, Window 
  7  from __pyjamas__ import doc, JS 
  8   
9 -class AlertHandler(Handler):
10 """A log output handler displaying any log message using an alert popup."""
11 - def __init__(self):
12 Handler.__init__(self)
13
14 - def emit(self, record):
15 msg = self.format(record) 16 Window.alert(msg)
17
18 -class AppendHandler(Handler):
19 """A log output handler showing text in a <div> appended to the end of the 20 HTML document. Use the 'div' property to find out the element's ID if you 21 want to position or style the output in your Pyjamas application.""" 22 div = None 23 div_id = '' 24 output = '' 25
26 - def __init__(self, logger_name):
27 Handler.__init__(self) 28 self.div_id = self.__getSafeNameFor('logging_' + logger_name)
29
30 - def __getSafeNameFor(self, name):
31 """Strip out all characters that could be invalid for an element ID""" 32 from string import ascii_letters, digits 33 return ''.join(c for c in name if c in (ascii_letters + digits + '_'))
34
35 - def __addLogElement(self):
36 """Add a container in the DOM where logging output will be written to. 37 This cannot be done in the constructor as it must happen late enough 38 to ensure a document body (to add an element to) does already exist.""" 39 if self.div == None: 40 self.div = DOM.createDiv() 41 self.div.setAttribute('id', self.div_id) 42 DOM.appendChild(doc().body, self.div)
43
44 - def emit(self, record):
45 msg = self.format(record) 46 msg = escape(msg) 47 msg = msg.replace("\n", "<br/>\n") + "<br/>\n" 48 self.output += msg 49 self.__addLogElement() 50 DOM.setInnerHTML(self.div, self.output)
51
52 -class ConsoleHandler(Handler):
53 """A log output handler making use of Firebug's console.log() function.""" 54 55 __consoleFuncForLevel = None 56
57 - def __init__(self):
58 Handler.__init__(self) 59 self.__consoleFuncForLevel = { 60 'DEBUG': self.__debug, 61 'INFO': self.__info, 62 'WARNING': self.__warn, 63 'ERROR': self.__error, 64 'CRITICAL': self.__error 65 }
66
67 - def emit(self, record):
68 """Print a message using the console.debug/info/warn/error/log() 69 functions. Use a simple print() as a fallback in browsers that don't 70 support console.log -- including Pyjamas Desktop.""" 71 msg = self.format(record) 72 func = self.__consoleFuncForLevel.get(record.levelname, self.__log) 73 try: 74 # NOTE: must be that clumsy, because JS() allows constants only!! 75 func(msg) 76 except: 77 print(msg)
78
79 - def __debug(self, msg):
80 JS(" console['debug'](@{{msg}}); ")
81
82 - def __info(self, msg):
83 JS(" console['info'](@{{msg}}); ")
84
85 - def __warn(self, msg):
86 JS(" console['warn'](@{{msg}}); ")
87
88 - def __error(self, msg):
89 JS(" console['error'](@{{msg}}); ")
90
91 - def __log(self, msg):
92 JS(" console['log'](@{{msg}}); ")
93
94 -class NullHandler(Handler):
95 """A log output handler that does nothing. Use to disable logging."""
96 - def __init__(self):
97 Handler.__init__(self)
98
99 - def emit(self, record):
100 pass
101