Created
October 25, 2018 01:58
-
-
Save carolineartz/5626972792fb1dac65e4eeec8ea108e0 to your computer and use it in GitHub Desktop.
Semantic Logger Cheatsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use blocks to prevent unnecessary evaluation | |
logger.debug do | |
count = results.inject(0) {|sum, i| i+sum } "A total of #{count} were processed" | |
end | |
# Measure the duration of any block of code | |
count = logger.benchmark_info('Counting users') do | |
User.where('created_at <= ?', date).count | |
end | |
# Only log when the block takes longer than 50ms | |
value = logger.benchmark_warning('Memcache call was slow', min_duration: 50) do | |
memcache.get('key') | |
end | |
# Add tags to any block of code | |
logger.tagged('127.0.0.1', 'jbloggs') do | |
logger.info 'Started processing normally' | |
logger.debug 'Calling supplier' | |
logger.trace 'Received Response', response | |
end | |
# Add the ip-address and a tracking number to every Rails message: | |
config.log_tags = [ | |
:remote_ip, | |
lambda do |request| | |
request.headers["Tracking-Number"] = random_tracking_number | |
end | |
] | |
# Log low level messages as trace | |
logger.trace('Received', raw_response) | |
# Semantic hashes | |
logger.error( | |
"Oops external call failed", | |
result: :failed, | |
reason_code: -10 | |
) | |
# Threads | |
# Name different threads | |
Thread.current.name = 'Main' | |
logger = SemanticLogger['Job'] | |
logger.info "Handing work to processing thread" | |
Thread.new do | |
Thread.current.name = 'Processor' | |
logger = SemanticLogger['User'] | |
logger.info "Started processing" | |
end | |
# Log rescued exceptions | |
begin | |
google.search('semantic logger') | |
rescue StandardError => exc | |
logger.error 'Failed calling supplier', exc | |
'No data available' | |
end | |
# Metrics | |
# Increment google search count metric | |
logger.info( | |
message: 'Called Google', | |
metric: 'google/search' | |
) | |
count = logger.benchmark_info('Counting users’, | |
metric: ‘user/count’) do | |
User.where('created_at <= ?', date).count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment