Tracks
/
Python
Python
/
Exercises
/
Log Levels
Log Levels

Log Levels

Work In Progress
Learning Exercise

Introduction

In Python, an enum is a set of names that are bound to unique literal, or constant values. Enums are defined by inheriting an Enum class. Built-in enum types are available in the module enum and the class Enum can be imported using from enum import Enum.

class Color(Enum):
    RED = 1
    GREEN = 2

Note that the values of the enum members can be any data types such as str, tuple, float, etc.

class Color(Enum):
    RED = 'red'
    GREEN = 'green'

When assigning the same value to two members in an enum, the latter assigned member will be an alias to the formed one. It is not allowed to use the same name for two members of an enum.

class Color(Enum):
    RED = 1
    GREEN = 2
    ALIAS_OF_RED = 1

Color.ALIAS_OF_RED
#=> <Color.RED: 1>

Color.ALIAS_OF_RED.value
#=> 1

Iterating through the members of the enum can be done with the standard for member in syntax:

for member in Color:
    print((member.name, member.value))
#=> (RED, 1)
#=> (GREEN, 2)

Enum members can be compared using is (identity operator) or is not. The == or != (equality_operators) work likewise.

a = Color.RED

a is Color.RED
#=> True

a == Color.RED
#=> True

To access an enum member for a given value, EnumName(value) can be used:

g = Color(2)

g is Color.GREEN
#=> True

g
#=> <Color.GREEN: 2>

Instructions

In this exercise, you'll be processing log messages with six severity levels.

Each log line is a string formatted as follows: "[<LVL>]: <MESSAGE>".

These are the different log levels:

LEVEL LVL
Trace TRC
Debug DBG
Info INF
Warning WRN
Error ERR
Fatal FTL

1. Parse log level

Define a LogLevel enum that has six elements corresponding to the log levels defined above. Next, define the parse_log_level function which takes the log message as parameter and returns the enum member of its level.

parse_log_level("[INF]: File deleted")
#=> LogLevel.Info

2. Support unknown log level

Unfortunately, some log messages occasionally appear with an unknown severity. To gracefully handle these 'mysterious' log messages in the function parse_log_level, add an Unknown member to the LogLevel enum which is returned when parsing an unknown log level:

parse_log_level("[XYZ]: Overly specific, out of context message")
#=> LogLevel.Unknown

3. Convert a log message to the short format

The log level of a log line is quite verbose. To reduce the disk space needed to store the log messages, a short format is defined: "[<CODE_LEVEL>]:<MESSAGE>".

The log level codes follow a straightforward mapping:

LEVEL CODE
Trace 0
Debug 1
Info 4
Warning 5
Error 6
Fatal 7
Unknown 42

Define the convert_to_short_log() function, which takes two parameters:

  1. Log level - The Log level of the log sent. ex: LogLevel.Error.
  2. Log Message - The message of type str.
convert_to_short_log(LogLevel.Error, "Stack overflow")
# => "6:Stack overflow"

4. Create an Alias

It looks like the user has created logs for LogLevel.Warn instead of LogLevel.Warning. Create an alias for LogLevel.Warning and return the new alias member in the function get_warn_alias.

This can be done on the same enum class LogLevel already defined at the top of the file. Both the LogLevels should point to same value: "WRN".

get_warn_alias()
#=> LogLevel.Warn

get_warn_alias() == LogLevel.Warning
#=> True

5. All Member Names and Values

Define the function get_members().

This function should return a list of tuples (name, value) containing all the members of the enum LogLevel.

get_members()
#=> [('Trace', 'TRC'), ('Debug', 'DBG'), ('Info', 'INF'), ('Warning', 'WRN'),
# ('Error', 'ERR'), ('Fatal', 'FTL'), ('Unknown', 'UKN')]
Edit via GitHub The link opens in a new window or tab
Python Exercism

Ready to start Log Levels?

Sign up to Exercism to learn and master Python with 17 concepts, 140 exercises, and real human mentoring, all for free.