profile
viewpoint
Zakaria Zajac madzak Brainfire, LLC Seattle, WA http://www.madzak.com

madzak/flask-foundation 10

Foundation framework integrated with flask

madzak/madzak.com 2

madzak.com site

madzak/alfred-spotify-controls 1

Control Spotify using Alfred App

madzak/chrome-headers-extension 1

Chrome Extension for settings headers on requests

madzak/creditgetter 1

A utility script I used in conjunction with wget to get copyright information from a website.

madzak/django-oauth2 1

OAuth2 Provider App for Django

madzak/django-slumlord 1

A multitenant solution for Django

madzak/dzen-utils 1

Disorderly Zen Utilities

madzak/haunt 1

A module for creating github issue bots

madzak/hoard-js 1

A Javascript Library for storing key/value or json objects in the browser provided databases

startedmadzak/python-json-logger

started time in 17 hours

fork bxxfighting/python-json-logger

Json Formatter for the standard python logger

fork in 19 hours

startedmadzak/python-json-logger

started time in 2 days

issue commentmadzak/python-json-logger

Examples of remapping fields?

There are few gotchas about the rename part. The case @evandam described will work fine, if format string contains relation to renamed fields, e.g.:

jsonlogger.JsonFormatter("%(levelname)s %(name)s", rename_fields={"levelname": "severity", "name": "logger"})

produces following output:

{"severity": "ERROR", "logger": "root"}

The use of name in format string may be misleading in this context, as we expect the key to be called logger after rename.

I recently run into similar issue trying to rename exc_info field like so:

jsonlogger.JsonFormatter("%(exc_info)s", rename_fields={"exc_info": "exception"})

This results in traceback being logged under exc_info key:

{"exception": ["<class 'Exception'>", "", "File "/.../main.py", line 14, in <module>\n raise Exception"], "exc_info": "Traceback (most recent call last):\n File "/.../main.py", line 14, in <module>\n raise Exception\nException"}

In this case, "hacking" format string does not work as expected, as we get exc_info logged anyway aside exception. Also its formatting differs - it's a list instead of string.

code snippet:

import logging
from pythonjsonlogger import jsonlogger

logger = logging.getLogger()

logHandler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter("%(exc_info)s", rename_fields={"exc_info": "exception"})
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)

try:
    raise Exception
except Exception:
    logger.exception("")
tejasmanohar

comment created time in 3 days

startedmadzak/python-json-logger

started time in 3 days

startedmadzak/python-json-logger

started time in 3 days

startedmadzak/python-json-logger

started time in 4 days

created repositoryDVG/tailwind-2-sketch-pallette

created time in 6 days

startedytdl-org/youtube-dl

started time in 7 days

created repositorytry-programming/001-ruby-basics

created time in 8 days

startedmadzak/python-json-logger

started time in 10 days

startedmadzak/python-json-logger

started time in 10 days

startedjoshlk/many_requests

started time in 10 days

startedmadzak/python-json-logger

started time in 11 days

issue openedmadzak/python-json-logger

[HELP] Logging Custom Python Class

I have a custom class that I'm trying to log:

class NameMatchDiagnostic():


    def __init__(self):
        self.row_data = {"key", "value"}
        self.table = "string"
        self.action = "string"
        self.db_record = {"key", "value"}
        self.top_matches = [{"key", "value"}, {"key", "value"}, {"key", "value"}]

All in all very basic class, I'm pulling out my hair trying to get this thing to work, ideally I'm trying to log a timestamp, message, and then this data.

Any help would be greatly appreciated.

created time in 13 days

issue closedmadzak/python-json-logger

[HELP] Logging Basic Python Class Only

I've got a basic python class that and an interpreter written that translates my class into JSON

class LogMessage:
    def __init__(self, message):
        self.timestamp = datetime.datetime.now()
        self.message = message

    def __str__(self):
        return f"{self.timestamp.isoformat()}: {self.message}"


class LogMessageEncoder(JSONEncoder):
    """
    JSON Encoder for Name Match Diagnostic Class
    """

    def default(self, o):
        # Rough conversion
        temp = o.__dict__
        for e in temp.keys():
            if isinstance(temp[e], datetime.datetime):
                temp[e] = temp[e].isoformat()
        return temp

logger = logging.getLogger()
logHandler = logging.FileHandler(f"logs/{datetime.datetime.today().strftime('%Y-%m-%dT%H-%M-%S')}-JSON-Log.log")
formatter = jsonlogger.JsonFormatter(json_encoder=modules.log_message.LogMessageEncoder)
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)
logger.setLevel(logging.DEBUG)

logger.debug(LogMessage("foo"))

# logs as {"message": "2020-11-13T14:45:56.602299: foo"}

How would I log just the class (and subclasses)? without something like "message" it seems that the logger is using the str representation instead of the encoder.

closed time in 13 days

TWDickson

issue openedmadzak/python-json-logger

[HELP] Logging Basic Python Class Only

I've got a basic python class that and an interpreter written that translates my class into JSON

`class LogMessage: def init(self, message): self.timestamp = datetime.datetime.now() self.message = message

def __str__(self):
    return f"{self.timestamp.isoformat()}: {self.message}"

class LogMessageEncoder(JSONEncoder): """ JSON Encoder for Name Match Diagnostic Class """

def default(self, o):
    # Rough conversion
    temp = o.__dict__
    for e in temp.keys():
        if isinstance(temp[e], datetime.datetime):
            temp[e] = temp[e].isoformat()
    return temp

logger = logging.getLogger() logHandler = logging.FileHandler(f"logs/{datetime.datetime.today().strftime('%Y-%m-%dT%H-%M-%S')}-JSON-Log.log") formatter = jsonlogger.JsonFormatter(json_encoder=modules.log_message.LogMessageEncoder) logHandler.setFormatter(formatter) logger.addHandler(logHandler) logger.setLevel(logging.DEBUG)

logger.debug(LogMessage("foo"))

logs as {"message": "2020-11-13T14:45:56.602299: foo"}

`

How would I log just the class (and subclasses)? without something like "message" it seems that the

created time in 13 days

issue commentmadzak/python-json-logger

Examples of remapping fields?

Hi folks,

I'm trying to use rename_fields like this but I can't seem to get it to work as expected?

I want to rename things like name and levelname similar to @fabito

jsonlogger.JsonFormatter(rename_fields={"levelname": "severity", "name": "logger"})
tejasmanohar

comment created time in 13 days

issue closedmadzak/python-json-logger

Installation with Python 3.7.7 and Pipenv

I cannot install the package with Python 3.7.7 and Pipenv.

I tried the solution from #100, but it doesn't work.

❯ pipenv install pythonjsonlogger
Installing pythonjsonlogger…
Error:  An error occurred while installing pythonjsonlogger!
Error text: Collecting pythonjsonlogger
  Using cached pythonjsonlogger-0.1.1.tar.gz (1.3 kB)
Building wheels for collected packages: pythonjsonlogger
  Building wheel for pythonjsonlogger (setup.py): started
  Building wheel for pythonjsonlogger (setup.py): finished with status 'error'
  Running setup.py clean for pythonjsonlogger
Failed to build pythonjsonlogger
Installing collected packages: pythonjsonlogger
    Running setup.py install for pythonjsonlogger: started
    Running setup.py install for pythonjsonlogger: finished with status 'error'

  ERROR: Command errored out with exit status 1:
   command: /Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py'"'"'; __file__='"'"'/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-wheel-z0mnneyx
       cwd: /private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/
  Complete output (31 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib
  creating build/lib/pythonjsonlogger
  copying pythonjsonlogger/__init__.py -> build/lib/pythonjsonlogger
  copying pythonjsonlogger/cli.py -> build/lib/pythonjsonlogger
  warning: build_py: byte-compiling is disabled, skipping.
  
  installing to build/bdist.macosx-10.15-x86_64/wheel
  running install
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py", line 47, in <module>
      'pythonjsonlogger = pythonjsonlogger.cli:cli',
    File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/setuptools/_distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 967, in run_commands
      self.run_command(cmd)
    File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
      cmd_obj.run()
    File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/wheel/bdist_wheel.py", line 326, in run
      self.run_command('install')
    File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/setuptools/_distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
      cmd_obj.run()
    File "/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py", line 20, in run
      raise Exception("You probably meant to install and run python-json-logger")
  Exception: You probably meant to install and run python-json-logger
  ----------------------------------------
  ERROR: Failed building wheel for pythonjsonlogger
DEPRECATION: Could not build wheels for pythonjsonlogger which do not use PEP 517. pip will fall back to legacy 'setup.py install' for these. pip 21.0 will remove support for this functionality. A possible replacement is to fix the wheel build issue reported above. You can find discussion regarding this at https://github.com/pypa/pip/issues/8368.
    ERROR: Command errored out with exit status 1:
     command: /Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py'"'"'; __file__='"'"'/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-record-cknflbcf/install-record.txt --single-version-externally-managed --compile --install-headers /Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/include/site/python3.7/pythonjsonlogger
         cwd: /private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/
    Complete output (14 lines):
    running install
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py", line 47, in <module>
        'pythonjsonlogger = pythonjsonlogger.cli:cli',
      File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/setuptools/_distutils/core.py", line 148, in setup
        dist.run_commands()
      File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 967, in run_commands
        self.run_command(cmd)
      File "/Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
        cmd_obj.run()
      File "/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py", line 20, in run
        raise Exception("You probably meant to install and run python-json-logger")
    Exception: You probably meant to install and run python-json-logger
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py'"'"'; __file__='"'"'/private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-install-6o24eqbd/pythonjsonlogger/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/gc/p51q0k_d0m72y9pz5ss3kkx00000gn/T/pip-record-cknflbcf/install-record.txt --single-version-externally-managed --compile --install-headers /Users/guillaume/.local/share/virtualenvs/my-app--UATg00V/include/site/python3.7/pythonjsonlogger Check the logs for full command output.

✘ Installation Failed 

closed time in 15 days

g-ongenae

issue commentmadzak/python-json-logger

Installation with Python 3.7.7 and Pipenv

Oh! That's embarrassing.

g-ongenae

comment created time in 15 days

startedmadzak/python-json-logger

started time in 15 days

issue commentmadzak/python-json-logger

Installation with Python 3.7.7 and Pipenv

the package name is "python-json-logger" not "pythonjsonlogger".

the error message you are getting is indicating the same:

Exception: You probably meant to install and run python-json-logger
g-ongenae

comment created time in 16 days

startedmadzak/python-json-logger

started time in 17 days

startedmadzak/python-json-logger

started time in 18 days

startedmadzak/python-json-logger

started time in 19 days

startedmadzak/python-json-logger

started time in 20 days

created repositoryLarouex/ArduinoOplaForAzureIoTCentral

created time in 21 days

startedmadzak/python-json-logger

started time in 21 days

startedmadzak/python-json-logger

started time in 21 days

created repositoryLarouex/ArduinoOplaForAzureIoTCentral

The Arduino Oplà IoT Kit working with Azure IoT Central for Cloud Telemetry

created time in 22 days

more