tm1py icon indicating copy to clipboard operation
tm1py copied to clipboard

Introduce `Tm1LogHandler` class

Open MariusWirtz opened this issue 3 years ago • 3 comments

Allow easy logging to TM1 Message Log with the standard python logging framework.

Sample:

import logging
import sys
from pathlib import Path

from TM1py import TM1Service
from TM1py.Utils import Tm1LogHandler


def configure_logging():
    log_file = Path(__file__).parent.joinpath(Path("simple_script.log"))

    logging.basicConfig(
        filename=str(log_file),
        format="%(asctime)s - %(levelname)s - %(message)s",
        level=logging.INFO,
    )
    # also log to stdout
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))


def add_tm1_logger(tm1: TM1Service):
    tm1_handler = Tm1LogHandler(tm1)

    tm1_handler.setLevel(logging.INFO)

    logging.getLogger().addHandler(tm1_handler)


if __name__ == "__main__":
    configure_logging()
    logging.info(f"Starting script: {__file__}")

    with TM1Service(base_url="https://localhost:12354", user="admin", password="apple") as tm1:
        add_tm1_logger(tm1)

        logging.info(f"Successfully connected to '{tm1.server.get_server_name()}' running version '{tm1.version}'")

MariusWirtz avatar Jan 27 '22 12:01 MariusWirtz

I built this for a project the other day in which we needed warning and error logs not just written to a local python log file but also delegated to the TM1 Message Log, which is monitored by humans and machines (e.g., PulseForTM1).

I wonder if this piece of code is generic or rather useful enough to be included in the TM1py Utils module. Obviously, we don't want to blow up the Utils module with stuff that is never actually used anywhere.

What are your thoughts on the Tm1LogHandler? Do you think it should be included?

MariusWirtz avatar Jan 27 '22 13:01 MariusWirtz

We use logguru for logging so if it can be added as a handler there I think that's a great idea. If I get some time I'll test it. Otherwise I highly recommend you check it out .

Sent from Workspace ONE Boxer

On Jan 27, 2022 5:02 AM, Marius Wirtz @.***> wrote:

I built this for a project the other day in which we needed warning and error logs to be delegated not just written to a local python log file, but also to the TM1 Message Log, which is monitored by humans and machines (e.g., PulseForTM1).

I wonder if this piece of code is generic or rather useful enough to be included in the TM1py Utils module. Obviously, we don't want to blow up the Utils module with stuff that is never actually used anywhere.

What are your thoughts on the Tm1LogHandler? Do you think it should be included?

Reply to this email directly, view it on GitHubhttps://github.com/cubewise-code/tm1py/pull/677#issuecomment-1023182742, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEK7GZSJMMHJARMRP3PD7MLUYE653ANCNFSM5M52LP2A. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you are subscribed to this thread.Message ID: @.***>

rclapp avatar Jan 27 '22 15:01 rclapp

Thanks, @rclapp. Logguru looks very human-friendly. That nice. I will check it out.

MariusWirtz avatar Jan 29 '22 21:01 MariusWirtz