Skip to main content

No project description provided

Project description

Lazy Mongo Log

Write to MongoDB as you print!

Installation

pip install lazy-mongo-log

How to Use

from pymongo import MongoClient
from lazy_mongo_log import LazyMongoLog

mongo = MongoClient("mongodb://localhost:27017/")
database = mongo["my-database"]
collection = database["my-collection"]

const log = LazyMongoLog(
    collection=collection,
)

log("Hello World!") # Hello World!

MongoDB

{
    "type": "info",
    "keyword": null,
    "message": "Hello World!",
    "date_created": 2023-01-08T06:42:01.003+00:00
}

Configuration

from pymongo import MongoClient
from lazy_mongo_log import LazyMongoLog

mongo = MongoClient("mongodb://localhost:27017/")
database = mongo["my-database"]
collection = database["my-collection"]

log = LazyMongoLog(
    # The MongoDB collection.
    collection=collection,

    # The default type when using the `print(...)`.
    # Other stuff like `print.error(...)` are not affected.
    type="super cool info",

    # Keyword to be included in the log.
    keyword="my cool keyword",

    # If this should also print on the console.
    use_console=True,

    # Don't like the log document schema?
    # You can change it here!
    log_selector=lambda document : {
        "super_message": document["message"],
        "secret_type": document["type"],
        # I don't want your damn keywords!
        "hello": "world!",
    },
)

Other Fun Stuff

Changing Configurations

If you want to set the configurations later, you can do it like so:

from pymongo import MongoClient
from lazy_mongo_log import LazyMongoLog

log = LazyMongoLog(
    # We can set the collection later.
    keyword="unicorns",
)

log("Hello World!") # Won't write to MongoDB...

mongo = MongoClient("mongodb://localhost:27017/")

database = mongo["my-database"]
collection = database["my-collection"]

log.set(
    collection=collection,
    keyword="dragons", # I want dragons instead.
)

log("Hello World!") # Now it does!

Branching Configurations

Do you only want to change the configuration for one specific thing? Here is how you do it:

from pymongo import MongoClient
from lazy_mongo_log import LazyMongoLog

log = LazyMongoLog(
    # We can set the collection later.
    keyword="unicorns",
)

log.using(
    keyword="dragons",
)("This is a dragon") # keyword = "dragons"

log("This is a unicorn.") # keyword = "unicorns"

log.using(
    type="my lair",
)("Welcome!") # type = "my lair"

log("Welcome back!") # type = "info"

# You can also do this!
log.using(
    keyword="snakes",
    type="sneaky",
).using(
    keyword="bears",
).using(
    collection=mySuperCoolCollection,
    keyword="why are you doing this?!",
).warn("Because, why not?")

Others

More tools to play with:

# Print using `info` type.
log.info("Hello %s!", "World") # MongoDB: type = "info"

# Print using `warning` type. Uses `console.warn(...)`.
log.warn("Tread lightly...") # MongoDB: type = "warning"

# Print using `error` type. Uses `console.error(...)`.
log.error("Something bad happened!") # MongoDB: type = "error"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lazy_mongo_log-0.1.7.tar.gz (2.9 kB view hashes)

Uploaded Source

Built Distribution

lazy_mongo_log-0.1.7-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page