πŸ“Logging

How to use Pylenium's built-in logger

Pylenium includes two custom Log Levels and a global Logger instance that you can use.

Log Levels

NameLevelNote

CRITICAL

50

ERROR

40

WARNING

30

USER

25

Custom

INFO

20

Default

COMMAND

15

Custom

DEBUG

10

If you are familiar with logging, then the above table is straightforward. If not, then all you really need to know about these levels is that you can set the Log Level when executing tests, and any logs at the specified level or higher will be captured.

For example, if you wanted to set the Log Level to see only logs at INFO and higher, you would do this:

pytest --pylog_level=INFO

The above command would ignore logs below the INFO level. In other words, ignore the COMMAND and DEBUG logs.

COMMAND Level

The COMMAND Log Level is used by Pylenium for logging its commands in a cleaner and easier to parse format. You shouldn't use this level unless you really want to. Take a look at our visit() command to see it in action:

def visit(self, url: str) -> "Pylenium":
    log.command("py.visit() - Visit URL: `%s`", url)
    self.webdriver.get(url)
    return self

Notice how the string uses the %s format and NOT the f-string format.

This is intentional!

USER Level

The USER Log Level is meant for you! This is a convenient way for logging things if you don't want everything from the INFO level.

I highly recommend creating your own loggers, but sometimes something simple like this is all you need πŸ˜„

To take advantage of this level, use log.this():

# You can import this in any file
from pylenium.log import logger as log

# Log this
def add_to_cart(item: str, quantity: int):
    log.this("Adding %s %s to my cart", quantity, item)
    ...

# Then call the function
add_to_cart("Charizard", 3)
>>> USER Adding 3 Charizard to my cart

You can also directly use py.log:

# Log this
def add_to_cart(py: Pylenium, item: str, quantity: int):
    py.log.this("Adding %s %s to my cart", quantity, item)
    ...

# Then call the function
add_to_cart(py, "Charizard", 3)
>>> USER Adding 3 Charizard to my cart

Last updated