Package: simple-slacklogger
Introslackloggeris a simple Python package that logs
application-level events to your Slack channels. The
package includes a function decorator, a regular
function, and some settings to play around with.
Here's how everything works.InstallationTo installslacklogger, use pip.pip install simple-slackloggerGetting StartedTo start usingslackloggeryou'll need to create
a Slack app in your Workspace. This is pretty
straightforward:Once you've signed up for a Slack account head
over to theSlack Apps page.Click onCreate New App, name your app, and
choose your Workspace.Click on theCreate Appbutton.Click onOauth & Permissionsin the left
sidebar.Add the'chat:write.public'Oauth Scope
under theBot Token Scopessection. This
will also add the'chat:write'scope.On the same page, at the top, click theInstall App to Workspacebutton.ClickAllowon the next screen.Save the generatedBot User OAuth Access Token.Head over to theSlack homepageand launch your workspace in your browser.Head to the channel where you want your app to
post logs, and note the path value in the URL after
the final'/'. It will look something likeD05ZAYNAASL. Copy this as well.Now that you have your Slackchannel_idandaccess_token, you can initializeslacklogger.import slacklogger

slacklogger.creds = {
    "channel_id": "YOUR_CHANNEL_ID",
    "access_token": "YOUR_ACCESS_TOKEN"
}Slacklogger DecoratorTheslacklogger.logdecorator wraps around any
function and runsbeforeyour main function.
For example:@slacklogger.log(message="It works")
def my_function():
    print("Hooray, it worked!")"It works" will be logged in your Slack channelbefore"Hooray, it works" is printed in your
app. If you want something more granular...Slacklogger Functionslacklogger.send_logworks much the same way as
the decorator, except it doesn't automatically
extract your function's name and script path -
you can do that with an included helper function,my_details, which takes the function's name as
a parameter.from slacklogger.helpers import my_details

def new_user(name: str):
    print("Creating new user...")
    func_name, script_path = my_details(new_user) 
    slacklogger.send_log(
        message=f"Created new user: {name}",
        function_name=func_name,
        script_path=script_path
    )Note that if your function is in a class, and you're
using themy_detailshelper function, prepend
your function's name withself- for example,my_details(self.new_user).from slacklogger.helpers import my_details

class User:
    def __init__(self):
        pass

    def new_user(self, name: str):
        print("Creating new user...")
        func_name, script_path = my_details(self.new_user) # <= add 'self'
        slacklogger.send_log(
            message=f"Created new user: {name}",
            function_name=func_name,
            script_path=script_path
        )Slacklogger OptionsFor both the decorator and the function,
only themessageparameter is
required, but you can also define thelevel, anytags, and thetimezone(this defaults to UTC).@slacklogger.log(
    message="My Message",
    level="info",
    tags=["#first", "#test"],
    timezone="America/New_York",
)
def my_function():
    print("Hooray, it worked!")This will output something akin to the
following in your Slack:The regular function has the same options, along withfunction_nameandscript_pathas we saw above.def new_user(name: str):
    print("Creating new user...")
    func_name, script_path = my_details(new_user) 
    slacklogger.send_log(
        message=f"Created new user: {name}",
        leve="info",
        tags=["#new_user", "#users"]
        function_name=func_name,
        script_path=script_path,
        timezone="America/New_York"
    )SettingsYou can define two settings after you enter
your credentials:date_formatandlevel_colors.
Thedate_formatsetting refers to how the log date and time
are displayed. Thelevel_colorssetting allows
you to define adictof level names and their
corresponding hex color codes.import slacklogger

slacklogger.creds = {
    "channel_id": "MY_CHANNEL_ID",
    "access_token": "MY_ACCESS_TOKEN"
}

slacklogger.settings = {
    "date_format": "%b %d, %Y | %H:%M:%S %Z",
    "level_colors": {
        "default": "#007300",
        "debug": "#007300",
        "info": "#0000e5",
        "warn": "#e5e500",
        "error": "#e59400",
        "fatal": "#ff0000"
    }
}The above values are the default settingsslackloggeruses.To create your own date formatting,
check outthis cheat sheet forstrftimedirectives.slacklogger.settings["date_format"] = "%b %d, %H:%M"Forlevel_colors, you can add any name and hex
color code you need toslacklogger.settings["level_colors"]- or
overwrite the entiredict.slacklogger.settings["level_colors"]["ntk"] = "#730073"You can usecolor-hex.comto choose
color hex codes for your levels.Using the example directly above, we can add
a new level ('ntk', or 'Nice to Know')
to the decorator or regular function.@slacklogger.log(
    message="NTK Example",
    level="ntk",
    tags=["#level", "#test"],
    timezone="America/New_York",
)
def my_function():
    print("New level acquired!")You'll see an output like this:TimezoneTo change the timezone, simply change thetimezoneparameter in either the decorator or the function.Check this Wiki linkfor relevant timezone names.def timezone_test(name: str):
    print("Testing new timezone...")
    slacklogger.send_log(
        message="Testing Timezone",
        level="debug",
        timezone="Europe/Lisbon" # <= change this
    )You should see something like this using the above
code (notice thatfunction_name,script_path,
andtagsare omitted, as they're all optional).