Robot Framework
librarylogger.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 
21 
22 import sys
23 import threading
24 
25 from robot.utils import console_encode
26 
27 from .logger import LOGGER
28 from .loggerhelper import Message
29 
30 
31 LOGGING_THREADS = ('MainThread', 'RobotFrameworkTimeoutThread')
32 
33 
34 def write(msg, level, html=False):
35  # Callable messages allow lazy logging internally, but we don't want to
36  # expose this functionality publicly. See the following issue for details:
37  # https://github.com/robotframework/robotframework/issues/1505
38  if callable(msg):
39  msg = str(msg)
40  if level.upper() not in ('TRACE', 'DEBUG', 'INFO', 'HTML', 'WARN', 'ERROR'):
41  raise RuntimeError("Invalid log level '%s'." % level)
42  if threading.current_thread().name in LOGGING_THREADS:
43  LOGGER.log_message(Message(msg, level, html))
44 
45 
46 def trace(msg, html=False):
47  write(msg, 'TRACE', html)
48 
49 
50 def debug(msg, html=False):
51  write(msg, 'DEBUG', html)
52 
53 
54 def info(msg, html=False, also_console=False):
55  write(msg, 'INFO', html)
56  if also_console:
57  console(msg)
58 
59 
60 def warn(msg, html=False):
61  write(msg, 'WARN', html)
62 
63 
64 def error(msg, html=False):
65  write(msg, 'ERROR', html)
66 
67 
68 def console(msg, newline=True, stream='stdout'):
69  msg = str(msg)
70  if newline:
71  msg += '\n'
72  stream = sys.__stdout__ if stream.lower() != 'stderr' else sys.__stderr__
73  stream.write(console_encode(msg, stream=stream))
74  stream.flush()
def trace(msg, html=False)
def warn(msg, html=False)
def info(msg, html=False, also_console=False)
def console(msg, newline=True, stream='stdout')
def debug(msg, html=False)
def write(msg, level, html=False)
def error(msg, html=False)
def console_encode(string, encoding=None, errors='replace', stream=sys.__stdout__, force=False)
Encodes the given string so that it can be used in the console.
Definition: encoding.py:61