The prefix module

The prefix module contains the Prefix, FnPrefix, and TimePrefix classes.

These classes provide the ability to create specific logging prefixes which will be printed at the start of a logged message. These prefixes can be things such as: the output of a specific function, the current time, or custom prefixes.

The FnPrefix class

Inheritance diagram of pyamp.logging.prefix.FnPrefix

class pyamp.logging.prefix.FnPrefix(function, color=0, prefix=None)[source]

The FnPrefix class provides to ability to create a Prefix whose text results from calling a specified function when the Prefix is applied to a message.

Example:

prefix = FnPrefix(function=lambda: "Hey!")

# Prints: [Hey!]: This is the message
prefix.apply("This is the message")

This class can be used to dynamically create the text used for a prefix by calling a function which returns the desired value for the prefix.

  • function – The function which returns the Prefix text
  • color – The color in which the Prefix will be displayed
  • prefix – The next Prefix in the chain

The Colors class

Inheritance diagram of pyamp.logging.prefix.Colors

class pyamp.logging.prefix.Colors

The Colors class contains two classes which define possible Foreground and Background colors. These colors can be used to colorize console output in a specific way.

class Background

The Background class contains definitions of the following background colors.

  • White_Bold_Underline
  • Blue, and Light_Blue
  • Purple, and Light_Purple
  • Green, and Light_Green
  • Red, and Light_Red
  • Gray, and Light_Gray
  • Cyan, and Light_Cyan
  • Orange, and Light_Yellow
class Colors.Foreground

The Foreground class contains definitions of the following foreground colors.

  • White
  • White_Bold
  • White_Underline
  • Black
  • Blue, and Light_Blue
  • Purple, and Light_Purple
  • Green, and Light_Green
  • Red, and Light_Red
  • Gray, and Light_Gray
  • Cyan, and Light_Cyan
  • Orange, and Light_Yellow

The Prefix class

Inheritance diagram of pyamp.logging.prefix.Prefix

class pyamp.logging.prefix.Prefix(text=None, color=0, prefix=None)[source]

The Prefix class provides the ability to add chain of prefixes to a message. A Prefix contains text which is printed in square brackets before the given message. The following shows an example of the a Prefix for the string “DEBUG”.

Example:

prefix = Prefix(text="DEBUG")

# Prints: [DEBUG]: This is the message
prefix.apply("This is the message")

A Prefix can be colored, and it can contain other prefixes which are appended to the message before adding the current Prefix.

An example of multiple prefixes:

debugPrefix = Prefix(text="DEBUG")
examplePrefix = Prefix(text="example", prefix=debugPrefix)
datePrefix = Prefix(text="03/11/2012", prefix=examplePrefix)

# Prints: [DEBUG]: [example]: [03/11/2012]: This is the message
datePrefix.apply("This is the message")
  • text – The text to display in the Prefix
  • color – The color in which the Prefix will be displayed
  • prefix – The next Prefix in the chain
apply(message, useColors=False)[source]

Apply the chain of Prefixes to the given message.

  • message – The message to apply the Prefix chain to
  • useColors – True to use colors, False otherwise

The TimePrefix class

Inheritance diagram of pyamp.logging.prefix.TimePrefix

class pyamp.logging.prefix.TimePrefix(format=None, color=0, prefix=None)[source]

The TimePrefix provides the ability to add a Prefix which displays the date and or time when the Prefix is applied to the message in a specified format.

Example:

prefix = TimePrefix(format="%Y-%m-%d")

# Prints: [2012-03-22]: This is the message
prefix.apply("This is the message")

This class is useful for dynamically timestamping logged messages so they can easily be tracked in the system output.

  • format – The format in which the time will be displayed. See the

    man page for ‘date’

  • color – The color in which the Prefix will be displayed

  • prefix – The next prefix in the chain

Table Of Contents

Previous topic

The logger module

Next topic

The loggable module

This Page