Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
|travis| |coveralls| |pypi| |downloads|
.. |travis| image:: https://travis-ci.org/keeprocking/pygelf.svg?branch=master :target: https://travis-ci.org/keeprocking/pygelf .. |pypi| image:: https://badge.fury.io/py/pygelf.svg :target: https://pypi.python.org/pypi/pygelf .. |coveralls| image:: https://coveralls.io/repos/github/keeprocking/pygelf/badge.svg?branch=master :target: https://coveralls.io/github/keeprocking/pygelf?branch=master .. |downloads| image:: https://img.shields.io/pypi/dm/pygelf.svg :target: https://pypi.python.org/pypi/pygelf
Python logging handlers with GELF (Graylog Extended Log Format) support.
Currently TCP, UDP, TLS (encrypted TCP) and HTTP logging handlers are supported.
.. code:: python
pip install pygelf
.. code:: python
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfTlsHandler, GelfHttpHandler
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger.addHandler(GelfTcpHandler(host='127.0.0.1', port=9401))
logger.addHandler(GelfUdpHandler(host='127.0.0.1', port=9402))
logger.addHandler(GelfTlsHandler(host='127.0.0.1', port=9403))
logger.addHandler(GelfHttpHandler(host='127.0.0.1', port=9404))
logger.info('hello gelf')
According to the GELF spec, each message has the following mandatory fields:
.. code:: python
try:
1/0
except ZeroDivisionError as e:
logger.exception(e)
.. _syslog-compliant: https://en.wikipedia.org/wiki/Syslog#Severity_level
In debug mode (when handler was created with debug=True option) each message contains some extra fields (which are pretty self-explanatory):
Each handler has the following parameters:
str
with exception for several :code:datetime
objects): function that is called for objects that cannot be serialized to JSON natively by python. Default implementation is custom function that returns result of :code:isoformat()
method for :code:datetime.datetime
, :code:datetime.time
, :code:datetime.date
objects and result of :code:str(obj)
call for other objects (which is string representation of an object with fallback to :code:repr
)Also, there are some handler-specific parameters.
UDP:
.. _MTU: https://en.wikipedia.org/wiki/Maximum_transmission_unit
TLS:
HTTP:
If you need to include some static fields into your logs, simply pass them to the handler constructor. Each additional field should start with underscore. You can't add field '_id'.
Example:
.. code:: python
handler = GelfUdpHandler(host='127.0.0.1', port=9402, _app_name='pygelf', _something=11)
logger.addHandler(handler)
If you need to include some dynamic fields into your logs, add them to record by using LoggingAdapter or logging.Filter and create handler with include_extra_fields set to True. All the non-trivial fields of the record will be sent to graylog2 with '_' added before the name
Example:
.. code:: python
class ContextFilter(logging.Filter):
def filter(self, record):
record.job_id = threading.local().process_id
return True
logger.addFilter(ContextFilter())
handler = GelfUdpHandler(host='127.0.0.1', port=9402, include_extra_fields=True)
logger.addHandler(handler)
If you need to include some fields from the environment into your logs, add them to record by using additional_env_fields
.
The following example will add an env
field to the logs, taking its value from the environment variable FLASK_ENV
.
.. code:: python
handler = GelfTcpHandler(host='127.0.0.1', port=9402, include_extra_fields=True, additional_env_fields={env: 'FLASK_ENV'})
logger.addHandler(handler)
The following can also be used in defining logging from configuration files (yaml/ini):
.. code:: ini
[formatters]
keys=standard
[formatter_standard]
class=logging.Formatter
format=%(message)s
[handlers]
keys=graylog
[handler_graylog]
class=pygelf.GelfTcpHandler
formatter=standard
args=('127.0.0.1', '12201')
kwargs={'include_extra_fields': True, 'debug': True, 'additional_env_fields': {'env': 'FLASK_ENV'}}
[loggers]
keys=root
[logger_root]
level=WARN
handlers=graylog
To run tests, you'll need tox_. After installing, simply run it:
.. code::
tox
.. _tox: https://pypi.python.org/pypi/tox
FAQs
Logging handlers with GELF support
We found that pygelf demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.