scripts/release/log.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 13 Mar 2009 23:01:11 +0000
changeset 1839 92b28d1bf2f5
parent 1834 0589bf1395c5
child 1846 ac30e04bcbba
permissions -rw-r--r--
Added example and help text to clarify the use of some fields on the proposal ranking. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1834
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     1
# Copyright 2009 the Melange authors.
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     2
#
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     3
# Licensed under the Apache License, Version 2.0 (the "License");
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     4
# you may not use this file except in compliance with the License.
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     5
# You may obtain a copy of the License at
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     6
#
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     7
#   http://www.apache.org/licenses/LICENSE-2.0
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     8
#
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     9
# Unless required by applicable law or agreed to in writing, software
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    10
# distributed under the License is distributed on an "AS IS" BASIS,
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    12
# See the License for the specific language governing permissions and
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    13
# limitations under the License.
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    14
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    15
"""Logging facilities.
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    16
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    17
The public interface is basically an initialization function for the
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    18
underlying Python logging module. Logging always goes to the console,
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    19
and can optionally be configured to write to a transcript file, which
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    20
will store exactly what appears on screen (minus colors).
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    21
"""
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    22
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    23
__authors__ = [
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    24
    # alphabetical order by last name, please
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    25
    '"David Anderson" <dave@natulte.net>',
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    26
    ]
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    27
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    28
import logging
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    29
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    30
import util
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    31
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    32
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    33
class _ColorizingFormatter(logging.Formatter):
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    34
  """A logging formatter that colorizes based on log levels."""
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    35
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    36
  def format(self, record):
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    37
    msg = logging.Formatter.format(self, record)
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    38
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    39
    if record.levelno >= logging.WARNING:
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    40
      return util.colorize(msg, util.RED, bold=True)
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    41
    elif record.levelno == logging.INFO:
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    42
      return util.colorize(msg, util.GREEN)
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    43
    else:
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    44
      return msg
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    45
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    46
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    47
class _DecolorizingFormatter(logging.Formatter):
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    48
  """A logging formatter that strips color."""
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    49
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    50
  def format(self, record):
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    51
    return util.decolorize(logging.Formatter.format(self, record))
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    52
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    53
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    54
def init(logfile=None):
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    55
  """Initialize the logging subsystem.
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    56
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    57
  Args:
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    58
    logfile: The filename for the transcript file, if any.
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    59
  """
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    60
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    61
  root = logging.getLogger('')
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    62
  root.setLevel(logging.DEBUG)
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    63
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    64
  console = logging.StreamHandler()
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    65
  console.setLevel(logging.DEBUG)
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    66
  console.setFormatter(_ColorizingFormatter())
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    67
  root.addHandler(console)
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    68
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    69
  if logfile:
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    70
    transcript = logging.FileHandler(logfile, 'w')
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    71
    transcript.setLevel(logging.DEBUG)
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    72
    transcript.setFormatter(_DecolorizingFormatter())
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    73
    root.addHandler(transcript)
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    74
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    75
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    76
debug = logging.debug
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    77
info = logging.info
0589bf1395c5 Add a log module to initialize and manage logging.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    78
error = logging.error