Skip to content

Commit

Permalink
Lumberaxe 🌲 (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Palhares <[email protected]>
  • Loading branch information
that-jill and xjunior authored Aug 15, 2022
1 parent 4c324c1 commit 2b3341e
Show file tree
Hide file tree
Showing 33 changed files with 480 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/lumberaxe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: lumberaxe

on:
push:

jobs:
ruby:
uses: ./.github/workflows/_ruby-package.yml
with:
package: ${{ github.workflow }}
ruby: '["2.7.4", "3.1.2"]'
rails: '["7.0.3.1","6.1.6.1","6.0.5.1","5.2.8.1"]'
secrets: inherit
8 changes: 8 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ power-tools currently contains the following packages (marked for release to rub

When included in a Rails application, NitroConfig loads the configuration file at `config/config.yml` within the application directory and makes its values available at `NitroConfig.config`. Config values are loaded based on the Rails environment, permitting the specification of multiple environments' configurations in a single file.

[audit_tracker](https://github.com/powerhome/power-tools/blob/main/packages/audit_tracker/docs/README.md) 💎

AuditTracker helps you centralize data tracking configuration to be used across different models.

[lumberaxe](https://github.com/powerhome/power-tools/blob/main/packages/lumberaxe/docs/README.md) 💎

Lumberaxe handles logging output formatting.

## Installation 🛠

These packages are all meant to install inside of an application and aren't intended to stand alone; currently, they are all published to [RubyGems](https://rubygems.org/) and you can use standard Bundler methods to install them.
Expand Down
14 changes: 14 additions & 0 deletions packages/lumberaxe/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/.bundle/
/.yardoc
/_yardoc/
coverage
pkg
/spec/reports/
**/tmp/*
!**/tmp/.gitkeep
!tmp/.gitignore
vendor/bundle
*.log
*.sqlite
*.sqlite3
Gemfile.lock
10 changes: 10 additions & 0 deletions packages/lumberaxe/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
inherit_from: .rubocop_todo.yml

require:
- rubocop-powerhome

AllCops:
TargetRubyVersion: 2.7

Rails:
Enabled: false
7 changes: 7 additions & 0 deletions packages/lumberaxe/.rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This configuration was generated by
# `rubocop --auto-gen-config --exclude-limit 500`
# on 2022-08-11 19:45:17 UTC using RuboCop version 1.29.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
13 changes: 13 additions & 0 deletions packages/lumberaxe/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

source "https://rubygems.org"
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end

gemspec

rails_version = ENV.fetch("RAILS_VERSION", ">= 5.2.8.1")

gem "rails", rails_version
31 changes: 31 additions & 0 deletions packages/lumberaxe/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env rake

# frozen_string_literal: true

require "rspec/core/rake_task"
require "rubocop/rake_task"

begin
require "bundler/setup"
rescue LoadError
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
end
begin
require "yard"
YARD::Rake::YardocTask.new do |t|
t.files = ["lib/**/*.rb"]
t.options = [
"--no-private",
]
end
rescue LoadError
warn "Could not require() YARD! Install with 'gem install yard' to get the 'yardoc' task"
end

Bundler::GemHelper.install_tasks

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

RuboCop::RakeTask.new(:rubocop)

task default: %i[spec rubocop]
9 changes: 9 additions & 0 deletions packages/lumberaxe/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "rubygems"
require "bundler"

Bundler.require :default, :development

Combustion.initialize! :all
run Combustion::Application
3 changes: 3 additions & 0 deletions packages/lumberaxe/doc/dependency_decisions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- - :inherit_from
- https://raw.githubusercontent.com/powerhome/oss-guide/master/license_rules.yml
23 changes: 23 additions & 0 deletions packages/lumberaxe/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Lumberaxe

Lumberaxe handles logging output formatting.

# Usage

After installing the gem, require it as part of your application configuration.

```ruby
# application.rb

require "lumberaxe"
```

## Setting JSON logging

To set up JSON logging on puma, add this to your puma config:

```ruby
# puma.rb

log_formatter(&Lumberaxe.puma_formatter)
```
20 changes: 20 additions & 0 deletions packages/lumberaxe/lib/lumberaxe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "logger"
require "active_support"
require "lumberaxe/railtie" if defined?(Rails)
require "lumberaxe/logger"
require "lumberaxe/json_formatter"

module Lumberaxe
def self.puma_formatter(level: "INFO", progname: "puma")
->(message) do
{
level: level,
time: Time.now,
progname: progname,
message: message,
}.to_json.concat("\r\n")
end
end
end
32 changes: 32 additions & 0 deletions packages/lumberaxe/lib/lumberaxe/json_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "active_support/tagged_logging"

module Lumberaxe
class JSONFormatter < ::Logger::Formatter
include ActiveSupport::TaggedLogging::Formatter

def call(severity, time, progname, data)
data = data.is_a?(Hash) ? format_data(data) : { message: data.to_s }

{
level: severity,
time: time,
progname: progname,
}.merge(data).to_json.concat("\r\n")
end

def format_data(data)
data.merge!(current_tags.each_with_object({}) do |tag, hash|
if tag.include?("=")
key, value = tag.split("=")
hash[key] = value
else
hash[:tags] ||= []
hash[:tags] << key
end
hash
end)
end
end
end
17 changes: 17 additions & 0 deletions packages/lumberaxe/lib/lumberaxe/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Lumberaxe
class Logger < ::ActiveSupport::Logger
cattr_accessor :log_level

def initialize(output = $stdout, progname:, level: log_level)
super output

self.progname = progname
self.level = level

self.formatter = JSONFormatter.new
extend ActiveSupport::TaggedLogging
end
end
end
26 changes: 26 additions & 0 deletions packages/lumberaxe/lib/lumberaxe/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "lograge"

module Lumberaxe
class Railtie < Rails::Railtie
initializer "lumberaxe.configurations", before: :initialize_logger do |app|
Rails.logger = app.config.logger || Lumberaxe::Logger.new(progname: "app", level: app.config.log_level)

app.config.log_tags = [
->(req) { "request_id=#{req.uuid}" },
->(req) { "IP=#{req.remote_ip}" },
]

Lumberaxe::Logger.log_level = app.config.log_level
end

initializer "lumberaxe.lograge" do
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Raw.new
config.lograge.custom_options = ->(event) do
{ "params" => event.payload[:params].without("controller", "action") }
end
end
end
end
5 changes: 5 additions & 0 deletions packages/lumberaxe/lib/lumberaxe/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module Lumberaxe
VERSION = "0.1.0"
end
53 changes: 53 additions & 0 deletions packages/lumberaxe/lumberaxe.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require_relative "lib/lumberaxe/version"

$LOAD_PATH.push File.expand_path("lib", __dir__)

Gem::Specification.new do |spec|
spec.name = "lumberaxe"
spec.version = Lumberaxe::VERSION
spec.authors = ["Carlos Palhares", "Jill Klang"]
spec.email = ["[email protected]", "[email protected]"]

spec.summary = "Power-ful logging wrapper"
spec.description = "Lumberaxe handles logging output formatting."
spec.homepage = "https://github.com/powerhome/power-tools"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.7"

spec.metadata["rubygems_mfa_required"] = "true"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/packages/lumberaxe/docs/CHANGELOG.md"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "activesupport", ">= 5.2.8.1"
spec.add_dependency "lograge", "0.10.0"

spec.add_development_dependency "bundler", "~> 2.1"
spec.add_development_dependency "combustion", "~> 1.3"
spec.add_development_dependency "license_finder", ">= 7.0"
spec.add_development_dependency "parser", ">= 2.5", "!= 2.5.1.1"
spec.add_development_dependency "pry-byebug", "3.9.0"
spec.add_development_dependency "rails", ">= 5.2.8.1"
spec.add_development_dependency "rainbow", "2.2.2"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rspec-rails", "~> 5.1.2"
spec.add_development_dependency "rubocop-powerhome", "0.5.0"
spec.add_development_dependency "simplecov", "0.15.1"
spec.add_development_dependency "sqlite3", "~> 1.4.2"
spec.add_development_dependency "test-unit", "3.1.5"
spec.add_development_dependency "yard", "0.9.21"
end
5 changes: 5 additions & 0 deletions packages/lumberaxe/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
site_name: Lumberaxe
nav:
- "Home": "README.md"
plugins:
- techdocs-core
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class CampgroundsController < ApplicationController
def create
campground_params = params.require(:campground).permit(:name)
campground = Campground.new(campground_params)

Rails.logger.warn("Creating campground named #{campground.name}")

if campground.save
render json: campground, status: :created
else
render json: campground.errors, status: :unprocessable_entity
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
7 changes: 7 additions & 0 deletions packages/lumberaxe/spec/internal/app/models/campground.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Campground < ApplicationRecord
self.table_name = "internal_campgrounds"

validates :name, presence: true
end
3 changes: 3 additions & 0 deletions packages/lumberaxe/spec/internal/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
adapter: sqlite3
database: db/combustion_test.sqlite
5 changes: 5 additions & 0 deletions packages/lumberaxe/spec/internal/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

Rails.application.routes.draw do
resources :campgrounds, only: :create
end
3 changes: 3 additions & 0 deletions packages/lumberaxe/spec/internal/config/storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
service: Disk
root: /Users/jill/power-tools/packages/lumberaxe/tmp/storage
8 changes: 8 additions & 0 deletions packages/lumberaxe/spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

ActiveRecord::Schema.define do
create_table(:internal_campgrounds, force: true) do |t|
t.string :name
t.timestamps
end
end
1 change: 1 addition & 0 deletions packages/lumberaxe/spec/internal/log/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
Empty file.
Loading

0 comments on commit 2b3341e

Please sign in to comment.