Skip to content

Instantly share code, notes, and snippets.

@nymous
nymous / README.md
Last active February 24, 2026 03:30
Logging setup for FastAPI, Uvicorn and Structlog (with Datadog integration)

Logging setup for FastAPI

This logging setup configures Structlog to output pretty logs in development, and JSON log lines in production.

Then, you can use Structlog loggers or standard logging loggers, and they both will be processed by the Structlog pipeline (see the hello() endpoint for reference). That way any log generated by your dependencies will also be processed and enriched, even if they know nothing about Structlog!

Requests are assigned a correlation ID with the asgi-correlation-id middleware (either captured from incoming request or generated on the fly). All logs are linked to the correlation ID, and to the Datadog trace/span if instrumented. This data "global to the request" is stored in context vars, and automatically added to all logs produced during the request thanks to Structlog. You can add to these "global local variables" at any point in an endpoint with `structlog.contextvars.bind_contextvars(custom

@panta
panta / logging.go
Last active May 27, 2025 21:01
zerolog with file log rotation (lumberjack) and console output
package logging
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"path"
"io"
)
@sobstel
sobstel / manual_preloading.rb
Created March 29, 2018 18:36
Rails manual association preloading
# taken from https://mrbrdo.wordpress.com/2013/09/25/manually-preloading-associations-in-rails-using-custom-scopessql/
# collection association e.g. has_many
owners = People.all
association_name = :photos
owners.each do |owner|
records = Array(whatever_you_want)
module Arel
module Visitors
class MyWhereSsqlWithoutSql < Arel::Visitors::ToSql
def initialize(inner_visitor, *args, &block)
@inner_visitor = inner_visitor
super(*args, &block)
end
private
@hadees
hadees / arel_helpers.rb
Created March 5, 2016 05:41
Arel Helpers
module ArelHelpers
extend self
def self.included(base)
base.extend self
end
def asterisk(arel_table_or_model)
arel_table, columns = case arel_table_or_model
when Arel::Table
@magnetikonline
magnetikonline / README.md
Last active May 21, 2023 08:15
macOS SSH client host autocomplete.

macOS SSH client host autocomplete

Snippet for ~/.bash_profile, adding hostname autocomplete to ssh and scp.

Extracts host hints from ~/.ssh/config.

function __completeSSHHosts {
  COMPREPLY=()
 local currentWord=${COMP_WORDS[COMP_CWORD]}
@forforf
forforf / stringify_hash.rb
Created August 21, 2012 21:11
Stringify Ruby Hash - One Liner
#From Sarah Mei
# http://stackoverflow.com/questions/800122/best-way-to-convert-strings-to-symbols-in-hash
my_hash.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
#To convert to symbols
my_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}