Skip to content

Data-driven log filter enabling logging custom fields from worker_ctx

License

Notifications You must be signed in to change notification settings

fraglab/nameko-log-context

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nameko-log-context

Data-driven log filter enabling logging custom fields from worker_ctx

Install

pip install nameko-log-context

Using

config.yml:

...
log_context:
    root_call_id: 'lambda worker_ctx: worker_ctx._call_id_stack[0]'
    call_id: 'lambda worker_ctx: worker_ctx.call_id'
...

LOGGING:
    filters:
        log_context:
            (): nameko_log_context.LogFilter
    handlers:
        handler:
            filters:
                - log_context
    formatters:
        formatter:
            format: '...(root_call_id),(call_id)...'

Service:

from nameko_log_context import LogContext
from nameko.rpc import rpc

class SomeService:
    log_context = LogContext()

    @rpc
    def method():
        """
        Every log message in this scope including logging in external
        packages is tagged with 'root_call_id' and 'call_id'
        """

How it works

  • Keys and lambda functions are retrieved on dependency provider setup
  • Log tags are evaluated in worker_setup on each request and placed in thread-local object
  • Log filter adds log tags from thread-local object to every log record

About

Data-driven log filter enabling logging custom fields from worker_ctx

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages