Step 1 - Simple Logging

In this lab, you will replace the print() statements, that send output to the console with logging, which sends the output to logs. Using logging allows us to return information about how the program is functioning. Using methods like print() allow us to return information to the user.

  • Create a new file called lab_8_step_1_simple_logging.py and add the following code:
import json

# This uses a json string as an input
json_string = """
{
    "Input":[
        {
        "Text":"I am learning to code in AWS",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        }
    ]
}
"""

json_input = json.loads(json_string)

# Defines two variables to store the language code from the input.
SourceLanguageCode = json_input['Input'][0]['SourceLanguageCode']
TargetLanguageCode = json_input['Input'][0]['TargetLanguageCode']

# The if statement checks to see if the language code is the same as the source code
if SourceLanguageCode == TargetLanguageCode:
    print("The SourceLanguageCode is the same as the TargetLanguageCode - stopping")
else:
    print("The Source Language and Target Language codes are different - proceeding")

We have used print() to output the messages to the console. This is where we need to make changes to the code to add them to logs rather than output to the console.

First, we need to import logging and then amend the print() statements with the relevant level for the message.

Amend the code as shown below to remove print() and replace it with logging.warning and logging.info .

# Import logging
import logging
import json

# This uses a json string as an input
json_string = """
{
    "Input":[
        {
        "Text":"I am learning to code in AWS",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        }
    ]
}
"""

json_input = json.loads(json_string)

# Defines two variables to store the language code from the input.
SourceLanguageCode = json_input['Input'][0]['SourceLanguageCode']
TargetLanguageCode = json_input['Input'][0]['TargetLanguageCode']

# The if statement checks to see if the language code is the same as the source code
if SourceLanguageCode == TargetLanguageCode:
    logging.warning("The SourceLanguageCode is the same as the TargetLanguageCode - stopping") # This will print to the console as the default level is warning
else:
    logging.info("The Source Language and Target Language codes are different - proceeding") # This will not print to the console because it is lower than warning
  • To run the program, enter the following command in the terminal:

    python lab_8_step_1_simple_logging.py

This should return nothing to the console. This is because by default the logging level is set to logging.warning. Because the code is correct, it does not generate a warning.

Test

Change the SourceLanguageCode to fr so that it is the same as the TargetLanguageCode and run the program again. This should return The SourceLanguageCode is the same as the TargetLanguageCode - stopping to the console.

What did we do?

  • We imported logging into our program.

  • We removed the print() statements and replaced them with logging.warning() and logging.info() respectively.

What did python do?

  • When python checked the if statement it only printed a message to the console when the if statement returned False. This is because logging.warning() will print to the console, otherwise the logging.info level will not return anything to the console.