Skip to content

Commit

Permalink
Replace Logger with Log (#1208)
Browse files Browse the repository at this point in the history
* Completely replace legacy Logger with Log

* fix specs

* convert severity from string to symbol

* fix severity

* disable Style/ConstantNames

* change string to Log::Severity enum value

* update ameba and fix redundant code
  • Loading branch information
drujensen authored Jun 10, 2020
1 parent 09654ce commit bd6e9a0
Show file tree
Hide file tree
Showing 44 changed files with 154 additions and 285 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ dependencies:
development_dependencies:
ameba:
github: veelenga/ameba
version: ~> 0.11.0
version: ~> 0.12.1
4 changes: 0 additions & 4 deletions spec/amber/amber_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ describe Amber do
Amber::Server.configure do |server|
server.name = "Hello World App"
server.port = 8080
server.logger = Amber::Environment::Logger.new(STDOUT)
server.logger.level = ::Log::Severity::Info
server.logging.colorize = false
server.logging.context = %w(request headers cookies session params)
server.logging.filter = %w(password confirm_password)
end

Expand All @@ -52,7 +49,6 @@ describe Amber do
settings.port.should eq 8080
settings.logging.colorize.should eq false
settings.secret_key_base.should eq "ox7cTo_408i4WZkKZ_5OZZtB5plqJYhD4rxrz2hriA4"
settings.logging.context.should eq %w(request headers cookies session params)
settings.logging.filter.should eq %w(password confirm_password)
end

Expand Down
4 changes: 0 additions & 4 deletions spec/amber/controller/base_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ module Amber::Controller
it "has a session id" do
controller.session.id.not_nil!.size.should eq 36
end

it "has logger" do
controller.logger.class.should eq Amber::Environment::Logger
end
end
end
end
59 changes: 0 additions & 59 deletions spec/amber/environment/logger_spec.cr

This file was deleted.

1 change: 0 additions & 1 deletion spec/amber/environment/settings_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Amber::Environment
test_yaml = File.read(File.expand_path("./spec/support/config/test.yml"))
settings = Amber::Settings.from_yaml(test_yaml)

settings.logger.should be_a Logger
settings.logging.severity.should eq Log::Severity::Warning
settings.logging.colorize.should eq true
settings.database_url.should eq "mysql://root@localhost:3306/test_settings_test"
Expand Down
1 change: 0 additions & 1 deletion spec/build_spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module Amber::CLI

prepare_yaml(Dir.current)
Amber::CLI.env = "test"
Amber::CLI.settings.logger = Amber::Environment::Logger.new(nil)
MainCommand.run ["db", "drop", "create", "migrate"]
end
end
2 changes: 0 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ CURRENT_DIR = Dir.current
Amber.path = "./spec/support/config"
Amber.env=(ENV["AMBER_ENV"])
Amber.settings.redis_url = ENV["REDIS_URL"] if ENV["REDIS_URL"]?
Amber::CLI.settings.logger = Amber::Environment::Logger.new(nil)
Amber.settings.logger = Amber::Environment::Logger.new(nil)

require "http"
require "spec"
Expand Down
6 changes: 0 additions & 6 deletions spec/support/config/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ logging:
filter:
- password
- confirm_password
context:
- request
- session
- headers
- cookies
- params

host: "localhost"
name: development_settings
Expand Down
6 changes: 0 additions & 6 deletions spec/support/config/fake_env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ logging:
filter:
- password
- confirm_password
context:
- request
- session
- headers
- cookies
- params

database_url: "mysql://root@localhost:3306/fake_settings_test"
host: 0.0.0.0
Expand Down
6 changes: 0 additions & 6 deletions spec/support/config/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ logging:
filter:
- password
- confirm_password
context:
- request
- session
- headers
- cookies
- params

database_url: "mysql://root@localhost:3306/test_settings_test"
host: 0.0.0.0
Expand Down
6 changes: 0 additions & 6 deletions spec/support/config/test_with_color.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ logging:
filter:
- password
- confirm_password
context:
- request
- session
- headers
- cookies
- params

host: 0.0.0.0
name: test_settings
Expand Down
12 changes: 12 additions & 0 deletions src/amber/cli.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
require "log"
require "cli"
require "./version"
require "./exceptions/*"
require "./environment"
require "./cli/commands"

backend = Log::IOBackend.new
backend.formatter = Log::Formatter.new do |entry, io|
io << entry.timestamp.to_s("%I:%M:%S")
io << " "
io << entry.source
io << " (#{entry.severity})" if entry.severity > Log::Severity::Debug
io << " "
io << entry.message
end
Log.builder.bind "*", :info, backend

Amber::CLI::MainCommand.run ARGV
6 changes: 4 additions & 2 deletions src/amber/cli/commands/command.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
abstract class Command < Cli::Command
Log = ::Log.for(self)

def info(msg)
Amber::CLI.logger.info msg, Class.name, :light_cyan
Log.info { msg.colorize(:light_cyan) }
end

def error(msg)
Amber::CLI.logger.error msg, Class.name, :red
Log.error { msg.colorize(:light_red) }
end
end
12 changes: 6 additions & 6 deletions src/amber/cli/commands/database.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "mysql"
require "sqlite3"

module Amber::CLI
CLI.logger.progname = "Database"
Log = ::Log.for("database")

class MainCommand < ::Cli::Supercommand
command "db", aliased: "database"
Expand Down Expand Up @@ -60,7 +60,7 @@ module Amber::CLI
create_database
when "seed"
Helpers.run("crystal db/seeds.cr", wait: true, shell: true)
CLI.logger.info "Seeded database"
info "Seeded database"
when "migrate"
migrate
when "rollback"
Expand Down Expand Up @@ -90,26 +90,26 @@ module Amber::CLI
if url.starts_with? "sqlite3:"
path = url.gsub("sqlite3:", "")
File.delete(path)
CLI.logger.info "Deleted file #{path}"
info "Deleted file #{path}"
else
name = set_database_to_schema url
Micrate::DB.connect do |db|
db.exec "DROP DATABASE IF EXISTS #{name};"
end
CLI.logger.info "Dropped database #{name}"
info "Dropped database #{name}"
end
end

private def create_database
url = Micrate::DB.connection_url.to_s
if url.starts_with? "sqlite3:"
CLI.logger.info CREATE_SQLITE_MESSAGE
info CREATE_SQLITE_MESSAGE
else
name = set_database_to_schema url
Micrate::DB.connect do |db|
db.exec "CREATE DATABASE #{name};"
end
CLI.logger.info "Created database #{name}"
info "Created database #{name}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/amber/cli/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Amber::CLI
begin
Config.from_yaml File.read(AMBER_YML)
rescue ex : YAML::ParseException
logger.error "Couldn't parse #{AMBER_YML} file", "Watcher", :red
Log.error(exception: ex) { "Couldn't parse #{AMBER_YML} file" }
exit 1
end
else
Expand Down
12 changes: 8 additions & 4 deletions src/amber/cli/generators.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require "./generators/**"

module Amber::CLI
class Generators
Log = ::Log.for("generate")

getter name : String
getter directory : String
getter fields : Array(String)
Expand Down Expand Up @@ -243,7 +245,7 @@ module Amber::CLI
info "Generating #{gen_class}"
gen_class.new(name, fields).render(directory, list: true, interactive: !options.assume_yes?, color: options.no_color?)
else
CLI.logger.error "Generator for #{command} not found", "Generate", :light_red
error "Generator for #{command} not found"
end
end

Expand All @@ -252,16 +254,18 @@ module Amber::CLI
end

def info(msg)
CLI.logger.info msg, "Generate", :light_cyan
Log.info { msg.colorize(:light_cyan) }
end

def error(msg)
CLI.logger.error msg, "Generate", :red
Log.error { msg.colorize(:light_red) }
end
end
end

class Teeplate::RenderingEntry
Log = ::Log.for("generate")

def appends?
@data.path.includes?("+")
end
Expand All @@ -279,7 +283,7 @@ class Teeplate::RenderingEntry
end

def list(s, color)
Amber::CLI.logger.info s.colorize.fore(color).to_s + local_path, "Generate", :light_cyan
Log.info { s.colorize.fore(color).to_s + local_path }
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/amber/cli/generators/auth.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ module Amber::CLI

private def inherit_plug(base, target)
routes = File.read("./config/routes.cr")
pipes = routes.match(/pipeline :#{base.to_s} do(.+?)end/m)
pipes = routes.match(/pipeline :#{base} do(.+?)end/m)
return unless pipes

replacement = <<-PLUGS
pipeline :#{base.to_s}, :#{target.to_s} do#{pipes[1]}
pipeline :#{base}, :#{target} do#{pipes[1]}
end
PLUGS
File.write("./config/routes.cr", routes.gsub(pipes[0], replacement))
Expand Down
12 changes: 6 additions & 6 deletions src/amber/cli/helpers/helpers.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Amber::CLI::Helpers
def add_routes(pipeline, route)
routes_file = File.read("./config/routes.cr")
routes = routes_file.match(/routes :#{pipeline.to_s}(.*?) do(.+?)end/m)
routes = routes_file.match(/routes :#{pipeline}(.*?) do(.+?)end/m)
if routes
replacement = <<-ROUTES
routes :#{pipeline.to_s}#{routes[1]} do
routes :#{pipeline}#{routes[1]} do
#{routes[2]}
#{route}
end
Expand All @@ -18,7 +18,7 @@ module Amber::CLI::Helpers
#{web_routes[2]}
end
routes :#{pipeline.to_s} do
routes :#{pipeline} do
#{route}
end
PLUGS
Expand All @@ -32,10 +32,10 @@ module Amber::CLI::Helpers
routes_file = File.read("./config/routes.cr")
return if routes_file.includes? plug

pipes = routes_file.match(/pipeline :#{pipeline.to_s}(.*?) do(.+?)end/m)
pipes = routes_file.match(/pipeline :#{pipeline}(.*?) do(.+?)end/m)
if pipes
replacement = <<-PLUGS
pipeline :#{pipeline.to_s}#{pipes[1]} do
pipeline :#{pipeline}#{pipes[1]} do
#{pipes[2]}
#{plug}
end
Expand All @@ -49,7 +49,7 @@ module Amber::CLI::Helpers
#{web_pipes[2]}
end
pipeline :#{pipeline.to_s} do
pipeline :#{pipeline} do
#{plug}
end
PLUGS
Expand Down
Loading

0 comments on commit bd6e9a0

Please sign in to comment.