Browse Source

Added rubocop and rufo deps

tags/v3.0.0
Lukas Zapletal 6 months ago
parent
commit
f5a2226586
13 changed files with 171 additions and 124 deletions
  1. +17
    -0
      .rubocop.yml
  2. +1
    -1
      Gemfile
  3. +8
    -1
      Rakefile
  4. +20
    -18
      journald-logger.gemspec
  5. +23
    -23
      lib/journald/classes/logger.rb
  6. +16
    -18
      lib/journald/classes/trace_logger.rb
  7. +8
    -8
      lib/journald/logger.rb
  8. +20
    -20
      lib/journald/modules/exceptionable.rb
  9. +17
    -16
      lib/journald/modules/loggable.rb
  10. +1
    -1
      lib/journald/modules/version.rb
  11. +22
    -0
      rakelib/rufo.rake
  12. +16
    -16
      spec/journald_logger_spec.rb
  13. +2
    -2
      spec/spec_helper.rb

+ 17
- 0
.rubocop.yml View File

@@ -0,0 +1,17 @@
AllCops:
Exclude:
- "spec/**/*"
- "vendor/**/*"
TargetRubyVersion: 2.0

Layout:
Enabled: false

Metrics:
Enabled: false

Style:
Enabled: false

Naming:
Enabled: false

+ 1
- 1
Gemfile View File

@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in journald-logger.gemspec
gemspec

+ 8
- 1
Rakefile View File

@@ -1 +1,8 @@
require 'bundler/gem_tasks'
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rubocop/rake_task"

RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new

task :default => [:spec, :rubocop, :'rufo:check']

+ 20
- 18
journald-logger.gemspec View File

@@ -1,26 +1,28 @@
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'journald/modules/version'
require "journald/modules/version"

Gem::Specification.new do |spec|
spec.name = 'journald-logger'
spec.version = Journald::Logger::VERSION
spec.authors = ['Anton Smirnov']
spec.email = ['[email protected]']
spec.summary = %q{systemd-journal native logger}
spec.homepage = 'https://github.com/theforeman/journald-logger'
spec.license = 'MIT'
spec.name = "journald-logger"
spec.version = Journald::Logger::VERSION
spec.authors = ["Anton Smirnov"]
spec.email = ["[email protected]"]
spec.summary = %q{systemd-journal native logger}
spec.homepage = "https://github.com/theforeman/journald-logger"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.files = `git ls-files -z`.split("\x0")
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.required_ruby_version = '>= 2.0.0'
spec.required_ruby_version = ">= 2.0.0"

spec.add_runtime_dependency 'journald-native', '~> 1.0'
spec.add_runtime_dependency "journald-native", "~> 1.0"

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec', '~> 3.4'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "rake"
spec.add_development_dependency "rufo"
spec.add_development_dependency "rubocop", "~> 0.50.0"
spec.add_development_dependency "rspec", "~> 3.4"
spec.add_development_dependency "simplecov"
end

+ 23
- 23
lib/journald/classes/logger.rb View File

@@ -5,7 +5,7 @@ module Journald
include Sysloggable

def initialize(progname = nil, min_priority = nil, **tags)
@tags = tags
@tags = tags
@logger = Native
self.min_priority = min_priority
self.progname = progname
@@ -36,7 +36,7 @@ module Journald
def print(priority, message)
send(
priority: priority,
message: message,
message: message,
)
end

@@ -81,36 +81,36 @@ module Journald

protected

# used internally by exception() and TraceLogger
def tag_trace_location(location)
tag code_file: location.path,
code_line: location.lineno,
code_func: location.label
end
# used internally by exception() and TraceLogger
def tag_trace_location(location)
tag code_file: location.path,
code_line: location.lineno,
code_func: location.label
end

def untag_trace_location
untag :code_file, :code_line, :code_func
end
def untag_trace_location
untag :code_file, :code_line, :code_func
end

private

def real_send(hash)
hash = hash.delete_if { |_, v| v.nil? }
def real_send(hash)
hash = hash.delete_if { |_, v| v.nil? }

array_to_send = hash.map do |k,v|
key = k.to_s.upcase
value = v.to_s
array_to_send = hash.map do |k, v|
key = k.to_s.upcase
value = v.to_s

if key == 'PRIORITY'
priority = value.to_i
if key == "PRIORITY"
priority = value.to_i

return 0 if priority > @min_priority # DEBUG = 7, ALERT = 1
end

"#{key}=#{value}"
return 0 if priority > @min_priority # DEBUG = 7, ALERT = 1
end

@logger.send(*array_to_send)
"#{key}=#{value}"
end

@logger.send(*array_to_send)
end
end
end

+ 16
- 18
lib/journald/classes/trace_logger.rb View File

@@ -5,25 +5,23 @@ module Journald
end

PASSTHROUGH_METHODS = [
:tag,
:tag_value,
:untag,
:progname,
:progname=,
:level,
:level=,
:sev_threshold,
:sev_threshold=,
:min_priority,
:min_priority=,
:tag,
:tag_value,
:untag,
:progname,
:progname=,
:level,
:level=,
:sev_threshold,
:sev_threshold=,
:min_priority,
:min_priority=,
]

METHODS = (
Journald::Logger. public_instance_methods(false) +
Journald::Logger::Exceptionable.public_instance_methods(false) +
Journald::Logger::Loggable. public_instance_methods(false) +
Journald::Logger::Sysloggable. public_instance_methods(false)
)
METHODS = (Journald::Logger.public_instance_methods(false) +
Journald::Logger::Exceptionable.public_instance_methods(false) +
Journald::Logger::Loggable.public_instance_methods(false) +
Journald::Logger::Sysloggable.public_instance_methods(false))

METHODS.each do |method|
if PASSTHROUGH_METHODS.include? method
@@ -32,7 +30,7 @@ module Journald
end
else
define_method(method) do |*args, &block|
@wrapped_logger.__send__(:tag_trace_location, caller_locations[0])
@wrapped_logger.__send__(:tag_trace_location, caller_locations(1..1).first)
@wrapped_logger.public_send(method, *args, &block)
end
end


+ 8
- 8
lib/journald/logger.rb View File

@@ -1,10 +1,10 @@
require 'logger'
require 'journald/native'
require "logger"
require "journald/native"

require_relative 'modules/version'
require_relative 'modules/exceptionable'
require_relative 'modules/loggable'
require_relative 'modules/sysloggable'
require_relative "modules/version"
require_relative "modules/exceptionable"
require_relative "modules/loggable"
require_relative "modules/sysloggable"

require_relative 'classes/logger'
require_relative 'classes/trace_logger'
require_relative "classes/logger"
require_relative "classes/trace_logger"

+ 20
- 20
lib/journald/modules/exceptionable.rb View File

@@ -8,30 +8,30 @@ module Journald

private

def real_exception(e, priority, is_cause)
# for Ruby 2.1 get cause if present
cause = if e.respond_to? :cause; e.cause; end
# for Ruby 2.1 get backtrace if present
bt = e.respond_to?(:backtrace_locations) &&
e.backtrace_locations &&
e.backtrace_locations.length > 0
def real_exception(e, priority, is_cause)
# for Ruby 2.1 get cause if present
cause = if e.respond_to? :cause; e.cause; end
# for Ruby 2.1 get backtrace if present
bt = e.respond_to?(:backtrace_locations) &&
e.backtrace_locations &&
e.backtrace_locations.length > 0

tag_trace_location(e.backtrace_locations[0]) if bt
tag_trace_location(e.backtrace_locations[0]) if bt

send(
priority: priority,
message: "Exception #{e.inspect}",
gem_logger_message_type: is_cause ? 'ExceptionCause' : 'Exception',
exception_class: e.class.name,
exception_message: e.message,
backtrace: bt ? e.backtrace.join("\n"): nil,
cause: cause ? cause.inspect : nil,
)
send(
priority: priority,
message: "Exception #{e.inspect}",
gem_logger_message_type: is_cause ? "ExceptionCause" : "Exception",
exception_class: e.class.name,
exception_message: e.message,
backtrace: bt ? e.backtrace.join("\n") : nil,
cause: cause ? cause.inspect : nil,
)

untag_trace_location if bt
untag_trace_location if bt

real_exception(cause, priority, true) if cause
end
real_exception(cause, priority, true) if cause
end
end
end
end

+ 17
- 16
lib/journald/modules/loggable.rb View File

@@ -5,22 +5,22 @@ module Journald

# our map differs from Syslog::Logger
LEVEL_MAP = {
::Logger::UNKNOWN => LOG_ALERT,
::Logger::FATAL => LOG_CRIT,
::Logger::ERROR => LOG_ERR,
::Logger::WARN => LOG_WARNING,
::Logger::INFO => LOG_INFO,
::Logger::DEBUG => LOG_DEBUG,
::Logger::UNKNOWN => LOG_ALERT,
::Logger::FATAL => LOG_CRIT,
::Logger::ERROR => LOG_ERR,
::Logger::WARN => LOG_WARNING,
::Logger::INFO => LOG_INFO,
::Logger::DEBUG => LOG_DEBUG,
}

def add(severity, message = nil, progname = nil, &block)
def add(severity, message = nil, progname = nil)
priority = severity_to_priority(severity) || LEVEL_MAP[::Logger::UNKNOWN]

# some black magic from Logger O__o
progname ||= self.progname
if message.nil?
if block_given?
message = block.call
message = yield
else
message = progname
progname = self.progname
@@ -28,9 +28,9 @@ module Journald
end

send(
priority: priority,
message: message,
syslog_identifier: progname,
priority: priority,
message: message,
syslog_identifier: progname,
)
end

@@ -38,7 +38,7 @@ module Journald

# add methods a la Logger.warn or Logger.error
::Logger::Severity::constants.each do |severity|
severity_key = severity.downcase
severity_key = severity.downcase
severity_value = ::Logger::Severity.const_get(severity)

define_method(severity_key) do |progname = nil, &block|
@@ -60,7 +60,7 @@ module Journald
# journald always logs everything
def level
priority = self.min_priority
LEVEL_MAP.select{|_,p| p <= priority}.keys.min
LEVEL_MAP.select { |_, p| p <= priority }.keys.min
end

def level=(severity)
@@ -101,9 +101,10 @@ module Journald
def close; end

private
def severity_to_priority(severity)
LEVEL_MAP[severity]
end

def severity_to_priority(severity)
LEVEL_MAP[severity]
end
end
end
end

+ 1
- 1
lib/journald/modules/version.rb View File

@@ -1,5 +1,5 @@
module Journald
class Logger
VERSION = '2.0.4'
VERSION = "2.0.4"
end
end

+ 22
- 0
rakelib/rufo.rake View File

@@ -0,0 +1,22 @@
desc "Alias for `rake rufo:run`"
task :rufo => ["rufo:run"]

namespace :rufo do
require "rufo"

def rufo_command(*switches, rake_args)
files_or_dirs = rake_args[:files_or_dirs] || "."
args = switches + files_or_dirs.split(" ")
Rufo::Command.run(args)
end

desc "Format Ruby code in current directory"
task :run, [:files_or_dirs] do |_task, rake_args|
rufo_command(rake_args)
end

desc "Check that no formatting changes are produced"
task :check, [:files_or_dirs] do |_task, rake_args|
rufo_command("--check", rake_args)
end
end

+ 16
- 16
spec/journald_logger_spec.rb View File

@@ -1,39 +1,39 @@
require 'spec_helper'
require "spec_helper"

RSpec.describe Journald::Logger do
describe 'acts as logger' do
describe "acts as logger" do
before(:each) do
allow(Journald::Native).to receive(:send).and_return(0)
@logger = Journald::Logger.new('rspec journald-logger', Journald::LOG_DEBUG)
@logger = Journald::Logger.new("rspec journald-logger", Journald::LOG_DEBUG)
end

it 'logs things as logger' do
it "logs things as logger" do
# todo: expects
@logger.warn 'test warn'
@logger.warn "test warn"
end

it 'logs things as syslog' do
it "logs things as syslog" do
# todo: expects
@logger.log_warning 'test log_warning'
@logger.log_warning "test log_warning"
end

it 'adds tags' do
@logger.tag(tag1: 'value1')
@logger.tag(tag2: 'value2')
it "adds tags" do
@logger.tag(tag1: "value1")
@logger.tag(tag2: "value2")

expect(@logger.tag_value(:tag1)).to eq('value1')
expect(@logger.tag_value(:tag1)).to eq("value1")

@logger.tag(tag1: 'in-block-value') do
expect(@logger.tag_value(:tag1)).to eq('in-block-value')
@logger.tag(tag1: "in-block-value") do
expect(@logger.tag_value(:tag1)).to eq("in-block-value")
end

expect(@logger.tag_value(:tag1)).to eq('value1') # expect old value to be restored
expect(@logger.tag_value(:tag1)).to eq("value1") # expect old value to be restored
end

it 'logs exceptions' do
it "logs exceptions" do
# todo: expects
begin
raise 'logs exception'
raise "logs exception"
rescue => e
@logger.exception e
end


+ 2
- 2
spec/spec_helper.rb View File

@@ -1,4 +1,4 @@
require 'simplecov'
require "simplecov"
SimpleCov.start

require_relative '../lib/journald/logger'
require_relative "../lib/journald/logger"

Loading…
Cancel
Save