-
Notifications
You must be signed in to change notification settings - Fork 26
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
Structured Logging #30
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 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 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 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,60 @@ | ||
# | ||
# Copyright:: Copyright (c) 2018 Chef Software, Inc. | ||
# License:: Apache License, Version 2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require "mixlib/log/logging" | ||
|
||
module Mixlib | ||
module Log | ||
class Child | ||
include Mixlib::Log::Logging | ||
|
||
attr_reader :parent | ||
def initialize(parent, metadata = {}) | ||
@parent = parent | ||
@metadata = metadata | ||
end | ||
|
||
def level | ||
parent.level | ||
end | ||
|
||
# Define the methods to interrogate the logger for the current log level. | ||
# Note that we *only* query the default logger (@logger) and not any other | ||
# loggers that may have been added, even though it is possible to configure | ||
# two (or more) loggers at different log levels. | ||
[:trace?, :debug?, :info?, :warn?, :error?, :fatal?].each do |method_name| | ||
define_method(method_name) do | ||
parent.send(method_name) | ||
end | ||
end | ||
|
||
def add(severity, message = nil, progname = nil, data: {}, &block) | ||
data = metadata.merge(data) if data.kind_of?(Hash) | ||
parent.send(:pass, severity, message, progname, data: data, &block) | ||
end | ||
|
||
def with_child(metadata = {}) | ||
child = Child.new(self, metadata) | ||
if block_given? | ||
yield child | ||
else | ||
child | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
require "logger" | ||
require "mixlib/log/logging" | ||
|
||
# A subclass of Ruby's stdlib Logger with all the mutex and logrotation stuff | ||
# ripped out, and metadata added in. | ||
module Mixlib | ||
module Log | ||
class Logger < ::Logger | ||
|
||
include Logging | ||
# | ||
# === Synopsis | ||
# | ||
# Logger.new(name, shift_age = 7, shift_size = 1048576) | ||
# Logger.new(name, shift_age = 'weekly') | ||
# | ||
# === Args | ||
# | ||
# +logdev+:: | ||
# The log device. This is a filename (String) or IO object (typically | ||
# +STDOUT+, +STDERR+, or an open file). | ||
# +shift_age+:: | ||
# Number of old log files to keep, *or* frequency of rotation (+daily+, | ||
# +weekly+ or +monthly+). | ||
# +shift_size+:: | ||
# Maximum logfile size (only applies when +shift_age+ is a number). | ||
# | ||
# === Description | ||
# | ||
# Create an instance. | ||
# | ||
def initialize(logdev) | ||
@progname = nil | ||
@level = DEBUG | ||
@default_formatter = Formatter.new | ||
@formatter = nil | ||
@logdev = nil | ||
if logdev | ||
@logdev = LocklessLogDevice.new(logdev) | ||
end | ||
end | ||
|
||
def add_data(severity, message, progname, data: {}) | ||
return true if @logdev.nil? || severity < @level | ||
data ||= {} | ||
if message.kind_of?(::Exception) | ||
data[:err] = message | ||
else | ||
data[:msg] = message | ||
end | ||
@logdev.write( | ||
format_message(format_severity(severity), Time.now, progname, data)) | ||
true | ||
end | ||
alias_method :add, :add_data | ||
|
||
class LocklessLogDevice < LogDevice | ||
|
||
def initialize(log = nil) | ||
@dev = @filename = @shift_age = @shift_size = nil | ||
if log.respond_to?(:write) && log.respond_to?(:close) | ||
@dev = log | ||
else | ||
@dev = open_logfile(log) | ||
@filename = log | ||
end | ||
@dev.sync = true | ||
end | ||
|
||
def write(message) | ||
@dev.write(message) | ||
rescue Exception => ignored | ||
warn("log writing failed. #{ignored}") | ||
end | ||
|
||
def close | ||
@dev.close rescue nil | ||
end | ||
|
||
private | ||
|
||
def open_logfile(filename) | ||
if FileTest.exist?(filename) | ||
open(filename, (File::WRONLY | File::APPEND)) | ||
else | ||
create_logfile(filename) | ||
end | ||
end | ||
|
||
def create_logfile(filename) | ||
logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT)) | ||
add_log_header(logdev) | ||
logdev | ||
end | ||
|
||
def add_log_header(file) | ||
file.write( | ||
"# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName] | ||
) | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# | ||
# Copyright:: Copyright (c) 2018 Chef Software, Inc. | ||
# License:: Apache License, Version 2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require "logger" | ||
|
||
module Mixlib | ||
module Log | ||
module Logging | ||
include ::Logger::Severity | ||
|
||
TRACE = -1 | ||
|
||
SEV_LABEL = %w{TRACE DEBUG INFO WARN ERROR FATAL ANY}.each(&:freeze).freeze | ||
|
||
def to_label(sev) | ||
SEV_LABEL[sev + 1] || -"ANY" | ||
end | ||
|
||
LEVELS = { :trace => TRACE, :debug => DEBUG, :info => INFO, :warn => WARN, :error => ERROR, :fatal => FATAL }.freeze | ||
LEVEL_NAMES = LEVELS.invert.freeze | ||
|
||
attr_accessor :metadata | ||
|
||
def pass(severity, args, progname = nil, data: {}, &block) | ||
args, progname, data = yield if block_given? | ||
add(severity, args, progname, data: data) | ||
end | ||
|
||
# Define the standard logger methods on this class programmatically. | ||
# No need to incur method_missing overhead on every log call. | ||
[:trace, :debug, :info, :warn, :error, :fatal].each do |method_name| | ||
level = LEVELS[method_name] | ||
define_method(method_name) do |msg = nil, data: {}, &block| | ||
pass(level, msg, data: data, &block) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
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.
this whole class is nifty.