Skip to content

Logging

prototype74 edited this page Dec 24, 2021 · 4 revisions

HyperUBot uses Python's built-in logger to log telethon and userbot events to the bot's Terminal and log file. Your modules can profit from it too e.g. to log errors, information about an action etc.

Logging format

Terminal logger and log file have a different log formation, where the Terminal has the basics while the file also include extra information like the device, version of HyperUBot, traceback to the file and it's line number etc.

Logging format in Terminal:
[DATE TIME] LEVEL: PROGRAM_NAME: MESSAGE

Logging format in log file:
[DATE TIME] PROCESS_ID LEVEL: PROGRAM_NAME: FUNCTION_NAME: MESSAGE [FILE_NAME:LINE_NUMBER]

Log levels

The following levels are being used by HyperUBot's logger: INFO, WARN, ERROR and CRITICAL. DEBUG is not being used as it may spam the terminal too much. All supported log levels do print the output in a different color:

  • I (INFO) -> Plain text (not colored)
  • W (WARN) -> Yellow colored output
  • E (ERROR) -> Red colored output
  • C (CRITICAL) -> Red background colored output

Note: on Windows: the package colorama is required in order to print colored text, without it the output stays in plain text

Use log in your modules

It's simple to use the logger in your modules by a simple import from logging package:

from logging import getLogger  # import getLogger

log = getLogger(__name__)  # pass the current module's name

You can also import log attribute from userbot. However using it will change the PROGRAM_NAME to userbot in the logging system which makes it a bit more difficult to traceback the program. For example the modules name is example. It should trace to userbot.modules_user.example, but will show userbot instead:

from userbot import log  # import log from userbot

log.info("Test log")

this will print [1970-01-01 12:00:00] I: userbot: Test log in your terminal and log file. This is because the log attribute were initialized at init part of the userbot. So we recommend always to initialize your own log attribute in your modules.

Example module with logging:

from userbot.sysutils.event_handler import EventHandler
from logging import getLogger

log = getLogger(__name__)
ehandler = EventHandler(log)

try:
    import xyz_module as my_module
except:
    log.critical("Couldn't import 'my_module'")

def test_func():
    print("hi")
    return

@ehandler.on(command="example", outgoing=True)
async def example(event):
    log.info("Running command '.example'")
    try:
        test_funk()
    except:
        log.warning("Oops! I couldn't say hi")

    try:
        await event.edit("This is an example!")
    except:
        log.error("Seems like I can't edit the message!")
    return

In Terminal it will look like this:

[1970-01-01 12:00:00] I: userbot.modules_user.example: Running command '.example'

In case calling test_func() didn't work:

[1970-01-01 12:00:00] W: userbot.modules_user.example: Oops! I couldn't say hi!

In case editing the message didn't work:

[1970-01-01 12:00:00] E: userbot.modules_user.example: Seems like I can't edit the message!

In case import xyz_module failed:

[1970-01-01 12:00:00] C: userbot.modules_user.example: Couldn't import 'my_module'

In log file:

[1970-01-01 12:00:00] 1234 I: userbot.modules_user.example: example: Running command '.example' [example.py:18]

In case calling test_func() didn't work:

[1970-01-01 12:00:00] 1234 W: userbot.modules_user.example: example: Oops! I couldn't say hi! [example.py:22]

In case editing the message didn't work:

[1970-01-01 12:00:00] 1234 E: userbot.modules_user.example: example: Seems like I can't edit the message! [example.py:27]

In case import xyz_module failed:

[1970-01-01 12:00:00] 1234 C: userbot.modules_user.example: <module>: Couldn't import 'my_module' [example.py:10]

Get or view hyper.log file

HyperUBot generates a log file hyper.log in it's root directory automatically everytime you run the bot. Restarting the bot will also reset the log file. To view the log file, locate to HyperUBot's root directory and run the following command in your terminal:

  • POSIX systems: cat hyper.log
  • Windows: Get-Content hyper.log

You can also send the log file in a Telegram chat to view it by running .sendlog in any chat.

The log file doesn't include any personal data unless you decide to log a lot of data in terminal with your modules but that would be non-sense. HyperUBot does log your Telegram user ID in terminal and log file but that's not a secret or counts as personal data as it is easy viewable with the help of a telegram bot or by third party applications. To keep it simple, it's safe to share the log file especially if you want to report a bug, need help to fix issues with your module or just to flex with your device hardware specifications.