TDKorn / my-magento

Python Magento 2 REST API Wrapper
https://my-magento.readthedocs.io
MIT License
11 stars 3 forks source link

Add MagentoLogger ๐Ÿ“ LoggerUtils โš™ and AuthenticationError โ›”๐Ÿ˜ฒ #4

Closed TDKorn closed 2 years ago

TDKorn commented 2 years ago

๐ŸŒŸFeature Update - Add Logging and (Better) Exceptions๐ŸŒŸ

Logging and exceptions - can you think of anything more exciting? ๐Ÿ˜ฉ

Major Changes

MagentoLogger

Summary (TL;DR)

  1. It's a custom logging class for the package that can be used like any other logger
  2. It has custom log message formatting and a bunch of extra helper methods (and static methods) built in

You can create a MagentoLogger from scratch, or you can call it from the package or a Client

# Use Client Logger
from magento import Client

>>> api = Client('website.com','username', 'password', login=False)
>>> api.logger.info('Hello from Client')

2022-06-17 00:18:07 INFO   |[ MyMagento | website_username ]|:  Hello from Client

# Use Package Logger
import magento
>>> magento.logger.info("Hello from my-magento")  # stdout_level is WARNING for package logger

>>> magento.logger.warning("Hello from my-magento")  

2022-06-17 00:16:06 WARNING  |[ MyMagento | my-magento ]|:  Hello from my-magento

# Create from scratch
from magento.utils import MagentoLogger    

>>> logger = MagentoLogger('magento_logger')
>>> logger.info('Hello')

2022-06-16 23:54:38 INFO   |[ MyMagento | magento_logger ]|:  Hello

>>> print(logger.handler_map)  # One of the many helper methods; maps handlers of a logger by type and name

{  
    'stream': {'MyMagento__magento_logger__INFO': <StreamHandler <stdout> (INFO)>},  # Default level is INFO
    'file': {  # FileHandlers are set to DEBUG; all handler names are based off stdout_level
        'MyMagento__magento_logger__INFO': { 
            'handler': <FileHandler C:\path\to\my-magento\magento_logger.log (DEBUG)>,
            'file': 'C:\\path\\to\\my-magento\\magento_logger.log'
        },
        'MyMagento__my-magento__WARNING': {  # Handler for the package, added to every MagentoLogger; stdout_level is WARNING 
            'handler': <FileHandler C:\path\to\my-magento\my-magento.log (DEBUG)>,
            'file': 'C:\\path\\to\\my-magento\\my-magento.log'
        }
    }
}

Important Info/Actual Details

from magento import Client

>>> api = Client('domain.com', 'username', 'password')

# Default Stdout Level is INFO
>>> api.logger.debug("AYOOO")

# Log level can be changed in place though
>>> api.logger.setup_logger(stdout_level="DEBUG")

True

# Or create and assign a new MagentoLogger object
api.logger = api.get_logger(stdout_level="DEBUG")

# Verify it worked
api.logger.debug("AYOOO")
>>> 2022-06-15 23:01:17 DEBUG  |[ MyMagento | domain_username ]|:  AYOOO

MagentoLogger Class Variables

You can use/edit the class variables below to change how names, messages, etc. look and function. Why class variables?

1) They can be accessed easily for formatting outside the class 2) They can be used for name checking outside the class 3) They can still reliably and effortlessly (!!!) do both of the above whenever I change formats

Using the class variables means only the variables themselves will need to be changed to make package-wide updates

class MagentoLogger:
    """Logging class used within the package

    :cvar PREFIX:           hardcoded prefix to use in log messages
    :cvar PACKAGE_LOG_NAME: the default name for the package logger
    :cvar CLIENT_LOG_NAME:  the default format for the client logger name
    :cvar LOG_MESSAGE:      the default format for the message component of log messages.
                            (Use magento.logger.LOG_MESSAGE for easy access)
    :cvar FORMATTER:        the default logging format
    :type FORMATTER:        logging.Formatter
    :cvar HANDLER_NAME      the default format for the names of handlers created by this package
    """

    PREFIX = "MyMagento"
    PACKAGE_LOG_NAME = "my-magento"
    CLIENT_LOG_NAME = "{domain}_{username}"
    HANDLER_NAME = '{}__{}__{}'.format(PREFIX, '{name}', '{stdout_level}')

    LOG_MESSAGE = "|[ {pfx} | {name} ]|:  {message}".format(
            pfx=PREFIX, name="{name}", message="{message}"
     )

     FORMATTER = logging.Formatter(
            fmt="%(asctime)s %(levelname)-5s  %(message)s",
            datefmt="%Y-%m-%d %H:%M:%S"
     )

AuthenticationError

Client

TDKorn commented 2 years ago

ok

TDKorn commented 2 years ago

PLS IGNORE ALL THE OCMMITS IM TRYING MY BEST