-
Notifications
You must be signed in to change notification settings - Fork 2
2001/no jira passion logger with context #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ColinDKelley
merged 27 commits into
master
from
2001/no-jira-passion-logger-with-context
Mar 2, 2020
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
559af35
no-jira: remove redundant namespace prefix
ColinDKelley 31fb5f9
no-jira: add missing tests for log_level; split add from add_if_enabled
ColinDKelley 6734bc7
no-jira: make explicit LoggerMixin so the ContextualLogger namespace …
ColinDKelley 04b4945
no-jira: make add_if_enabled to separate out log_level_enabled? from add
ColinDKelley 0b3ada9
no-jira: omit progname: when it's null
ColinDKelley e51d702
no-jira: move logdev check into write_entry_to_log and make it public
ColinDKelley 099cd68
no-jira: add LoggerWithContext and its spec
ColinDKelley 2e10bb6
no-jira: DRY up Helpers
ColinDKelley 7f8d2be
no-jira: add test for cache clearing at 5000 entries
ColinDKelley 24df4fb
no-jira: add spaces before braces
ColinDKelley 06266f3
no-jira: move spec/{context,mixins} down under contextual_logger/ to …
ColinDKelley a6400d5
no-jira: change override_level to allow nil (the default) and then de…
ColinDKelley 25c9cb4
no-jira: indent LoggerMixin
ColinDKelley 90388c4
no-jira: clarify the with_context contract and unify the block_given?…
ColinDKelley 902a561
no-jira: restore add signature since Rails calls it
ColinDKelley 8579960
no-jira: fix to use context: kwarg
ColinDKelley d15922e
no-jira: fix tests to tolerate different JSON key order
ColinDKelley dad9982
no-jira: cap the merge cache at 1000; beyond that, still read, but do…
ColinDKelley 13bd8dd
no-jira: remove unused context; ArgumentError if logger doesn't have …
ColinDKelley 3defc36
no-jira: rename to message_hash_with_context; normalize arg order; do…
ColinDKelley 90b1301
no-jira: plumb level: through for_log_source
ColinDKelley fe17c6d
no-jira: bump version to 0.4.pre.1
ColinDKelley 469dea0
no-jira: add Rakefile and rake
ColinDKelley 84c5da9
no-jira: add allowed_push_host
ColinDKelley 86ed9d5
allow string log levels
ColinDKelley 646ad06
bump version to 0.4.pre.2
ColinDKelley 5734bd3
remove Logger typo
ColinDKelley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env rake | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require 'rubygems' | ||
require 'bundler/setup' | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLines: Extra blank line detected. |
||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task test: :spec | ||
task default: :spec |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module ContextualLogger | ||
# A logger that deep_merges additional context and then delegates to the given logger. | ||
# Keeps it own log level (called override_level) that may be set independently of the logger it delegates to. | ||
# If override_level is non-nil, it takes precedence; if it is nil (the default), then it delegates to the logger. | ||
class LoggerWithContext | ||
include LoggerMixin | ||
|
||
attr_reader :logger, :override_level, :context | ||
|
||
def initialize(logger, context, level: nil) | ||
logger.is_a?(LoggerMixin) or raise ArgumentError, "logger must include ContextualLogger::LoggerMixin (got #{logger.inspect})" | ||
@logger = logger | ||
self.level = level | ||
@context = context | ||
@merged_context_cache = {} # so we don't have to merge every time | ||
end | ||
|
||
def level | ||
@override_level || @logger.level | ||
end | ||
|
||
def level=(override_level) | ||
ColinDKelley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@override_level = | ||
if (Logger::Severity::DEBUG..Logger::Severity::UNKNOWN).include?(override_level) || override_level.nil? | ||
override_level | ||
else | ||
case override_level.to_s.downcase | ||
when 'debug' | ||
Logger::Severity::DEBUG | ||
when 'info' | ||
Logger::Severity::INFO | ||
when 'warn' | ||
Logger::Severity::WARN | ||
when 'error' | ||
Logger::Severity::ERROR | ||
when 'fatal' | ||
Logger::Severity::FATAL | ||
when 'unknown' | ||
Logger::Severity::UNKNOWN | ||
else | ||
raise ArgumentError, "invalid log level: #{override_level.inspect}" | ||
end | ||
end | ||
end | ||
|
||
def write_entry_to_log(severity, timestamp, progname, message, context:) | ||
merged_context = | ||
if @merged_context_cache.size >= 1000 # keep this cache memory use finite | ||
@merged_context_cache[context] || @context.deep_merge(context) | ||
else | ||
@merged_context_cache[context] ||= @context.deep_merge(context) | ||
end | ||
|
||
@logger.write_entry_to_log(severity, timestamp, progname, message, context: merged_context) | ||
end | ||
|
||
class << self | ||
def for_log_source(logger, log_source, level: nil) | ||
new(logger, { log_source: log_source }, level: level) | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lint/ScriptPermission: Script file Rakefile doesn't have execute permission.