GithubHelp home page GithubHelp logo

tidbits's Introduction

Useful Python Tidbits

Deprecation Notice

The various features implemented in this library have now been upstreamed in the revelant packages:

  • sentry-sdk has replaced Raven and solves the problem outlined below
  • python-json-logger now has single-line support for setting rename_fields={'levelname': 'severity'} which is enough to fix Stackdriver logging

This library is now deprecated in favour of using the upstream features directly!

Original README

Ever find yourself copying and pasting small chunks of Python code between a bunch of your projects? The goal of this library is to reduce or eliminate that problem.

Yes, I realize that's never going to happen. Contributions are very appreciated though, let's see how close we can get.

Latest PyPI Version CircleCI Test Status Python Version Support

Installation

$ pip install --upgrade tidbits

Note that in an effort to keep this library generalized, optional dependencies are not included, ie. for the Sentry integration you will still need to

$ pip install raven

For convenience, these are bundled as extra dependencies, ie. you can do

$ pip install --upgrade tidbits[gcloud,sentry]

Usage

GCloud

> requires python-json-logger or tidbits[gcloud]

Have you ever run an app on Google Cloud and wondered why all your logs were marked as errors, regardless of log level? Well, most likely you weren't formatting them correctly -- Google Cloud expects JSON logs with "severity" rather than "levelname", which you can set with:

from tidbits.gcloud.log import get_handler

logging.getLogger().addHandler(get_handler())
# or, even better, set all your loggers at once with tidbits.log

Log

Ever had to configure the loggers of all of your dependencies? This one's for you.

from tidbits.log import get_all_loggers
from tidbits.log import get_all_base_loggers
from tidbits.log import set_handler
from tidbits.log import set_loglevel

import requests

get_all_loggers()
# ['requests', 'urllib3', 'urllib3.connection', 'urllib3.connectionpool', 'urllib3.poolmanager', 'urllib3.response', 'urllib3.util', 'urllib3.util.retry']

get_all_base_loggers()
# ['requests', 'urllib3']

set_handler(myCoolHandler, logger='applesauce')
# the "applesauce" logger uses this (and only this) handler
set_handler_globally(myCoolHandler)
# all loggers use this (and only this) handler
set_handler_globally(myCoolHandler, ignore={'aardvark', 'banana'})
# all loggers except those listed use this (and only this) handler

set_loglevel(debug=True)
# sets logging.getLogger() to DEBUG and all others to INFO
set_loglevel(debug=False)
# sets logging.getLogger() to INFO and all others to WARNING

Integrations

> requires raven or tidbits[sentry]

Do you use Sentry? I do. And every single project I use it in contains the same block for configuring it and instrumenting the error logger.

from tidbits.integration.sentry import SENTRY
from tidbits.integration.sentry import instrument_logger

try:
    {}['missing_key']
except Exception:
    # damn, I totally didn't expect an error here, better send it to Sentry
    SENTRY.captureException()

# creates events in Sentry for each error log
instrument_logger(level=logging.Error)

tidbits's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

tidbits's Issues

chore(dependencies): Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>thekevjames/tools:personal)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.