Skip to content

Commit

Permalink
refactor(logger): Renamed feature logger to logging to better accept …
Browse files Browse the repository at this point in the history
…logger format
  • Loading branch information
icyleaf committed Aug 31, 2018
1 parent cfaddb6 commit 1ec66b8
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 73 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ We can enable per operation logging by configuring them through the chaining API
By default, Halite will logging all outgoing HTTP requests and their responses(without binary stream) to `STDOUT` on DEBUG level.
You can configuring the following options:

- `logger`: Instance your `Halite::Features::Logger::Abstract`, check [Use the custom logger](#use-the-custom-logger).
- `logger`: Instance your `Halite::Logger::Abstract`, check [Use the custom logger](#use-the-custom-logger).
- `format`: Outputing format, built-in `common` and `json`, you can write your own.
- `file`: Write to file with path, works with `format`.
- `filemode`: Write file mode, works with `format`, by default is `a`. (append to bottom, create it if file is not exist)
Expand Down Expand Up @@ -588,11 +588,11 @@ Halite.logger(format: "json", file: "logs/halite.log")

#### Use the custom logger

Creating the custom logger by integration `Halite::Features::Logger::Abstract` abstract class.
Creating the custom logger by integration `Halite::Logger::Abstract` abstract class.
Here has two methods must be implement: `#request` and `#response`.

```crystal
class CustomLogger < Halite::Features::Logger::Abstract
class CustomLogger < Halite::Logging::Abstract
def request(request)
@logger.info "| >> | %s | %s %s" % [request.verb, request.uri, request.body]
end
Expand All @@ -603,7 +603,7 @@ class CustomLogger < Halite::Features::Logger::Abstract
end
# Add to adapter list (optional)
Halite::Logger.register_adapter "custom", CustomLogger.new
Halite::Logging.register "custom", CustomLogger.new
Halite.logger(logger: CustomLogger.new)
.get("http://httpbin.org/get", params: {name: "foobar"})
Expand All @@ -623,7 +623,7 @@ in your HTTP client and allowing you to monitor outgoing requests, and incoming

Avaiabled features:

- logger (Cool, aha!)
- logging (Cool, aha!)

#### Write a simple feature

Expand All @@ -645,7 +645,7 @@ class RequestMonister < Halite::Feature
request
end
Halite::Features.register "request_monster", self
Halite.register_feature "request_monster", self
end
```

Expand Down Expand Up @@ -685,7 +685,7 @@ class AlwaysNotFound < Halite::Feature
chain.next(response)
end
Halite::Features.register "404", self
Halite.register_feature "404", self
end
class PoweredBy < Halite::Feature
Expand All @@ -698,7 +698,7 @@ class PoweredBy < Halite::Feature
end
end
Halite::Features.register "powered_by", self
Halite.register_feature "powered_by", self
end
r = Halite.use("404").use("powered_by").get("http://httpbin.org/user-agent")
Expand Down
16 changes: 8 additions & 8 deletions spec/halite/features/logger_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

private class SimpleLogger < Halite::Logger::Abstract
private class SimpleLogger < Halite::Logging::Abstract
def request(request)
@logger.info "request"
end
Expand All @@ -9,26 +9,26 @@ private class SimpleLogger < Halite::Logger::Abstract
@logger.info "response"
end

Halite::Logger.register "simple", self
Halite::Logging.register "simple", self
end

describe Halite::Logger do
describe Halite::Logging do
it "should register an format" do
Halite::Logger["simple"].should eq(SimpleLogger)
Halite::Logger.availables.should eq ["common", "json", "simple"]
Halite::Logging["simple"].should eq(SimpleLogger)
Halite::Logging.availables.should eq ["common", "json", "simple"]
end

it "should use common as default logger" do
logger = Halite::Logger.new
logger.writer.should be_a(Halite::Logger::Common)
logger = Halite::Logging.new
logger.writer.should be_a(Halite::Logging::Common)
logger.writer.skip_request_body.should be_false
logger.writer.skip_response_body.should be_false
logger.writer.skip_benchmark.should be_false
logger.writer.colorize.should be_true
end

it "should use custom logger" do
logger = Halite::Logger.new(logger: SimpleLogger.new)
logger = Halite::Logging.new(logger: SimpleLogger.new)
logger.writer.should be_a(SimpleLogger)
logger.writer.skip_request_body.should be_false
logger.writer.skip_response_body.should be_false
Expand Down
30 changes: 15 additions & 15 deletions spec/halite/options_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private class SimpleFeature < Halite::Feature
Halite.register_feature "simple", self
end

private class SimpleLogger < Halite::Logger::Abstract
private class SimpleLogger < Halite::Logging::Abstract
def request(request)
@logger.info "request"
end
Expand All @@ -22,7 +22,7 @@ private class SimpleLogger < Halite::Logger::Abstract
@logger.info "response"
end

Halite::Logger.register "simple", self
Halite::Logging.register "simple", self
end

private def test_options
Expand Down Expand Up @@ -256,25 +256,25 @@ describe Halite::Options do
describe "#with_logger" do
it "should overwrite logger with instance class" do
options = Halite::Options.new.with_logger(logger: SimpleLogger.new)
logger = options.features["logger"].as(Halite::Logger)
logger = options.features["logging"].as(Halite::Logging)
logger.writer.should be_a(SimpleLogger)
end

it "should overwrite logger with format name" do
Halite::Logger.register "simple", SimpleLogger
Halite::Logging.register "simple", SimpleLogger

options = Halite::Options.new.with_logger(format: "simple")
logger = options.features["logger"].as(Halite::Logger)
logger = options.features["logging"].as(Halite::Logging)
logger.writer.should be_a(SimpleLogger)
end

it "should became a file logger" do
Halite::Logger.register "simple", SimpleLogger
Halite::Logging.register "simple", SimpleLogger

tempfile = Tempfile.new("halite_logger")

options = Halite::Options.new.with_logger(format: "simple", file: tempfile.path, filemode: "w")
logger = options.features["logger"].as(Halite::Logger)
logger = options.features["logging"].as(Halite::Logging)
logger.writer.should be_a(SimpleLogger)
end

Expand All @@ -287,23 +287,23 @@ describe Halite::Options do

describe "#with_features" do
it "should use a feature" do
options = Halite::Options.new.with_features("logger")
logger = options.features["logger"].as(Halite::Logger)
logger.writer.should be_a(Halite::Logger::Common)
options = Halite::Options.new.with_features("logging")
logger = options.features["logging"].as(Halite::Logging)
logger.writer.should be_a(Halite::Logging::Common)
end

it "should use a feature with options" do
options = Halite::Options.new.with_features("logger", logger: SimpleLogger.new)
logger = options.features["logger"].as(Halite::Logger)
options = Halite::Options.new.with_features("logging", logger: SimpleLogger.new)
logger = options.features["logging"].as(Halite::Logging)
logger.writer.should be_a(SimpleLogger)
end

it "should use multiple features" do
Halite.register_feature "simple", SimpleFeature

options = Halite::Options.new.with_features("logger", "simple")
logger = options.features["logger"].as(Halite::Logger)
logger.writer.should be_a(Halite::Logger::Common)
options = Halite::Options.new.with_features("logging", "simple")
logger = options.features["logging"].as(Halite::Logging)
logger.writer.should be_a(Halite::Logging::Common)

simple = options.features["simple"].as(SimpleFeature)
simple.should be_a(SimpleFeature)
Expand Down
20 changes: 10 additions & 10 deletions spec/halite_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,23 @@ describe Halite do
describe ".use" do
describe "built-in features" do
it "sets given feature name" do
client = Halite.use("logger")
client.options.features.has_key?("logger").should be_true
client.options.features["logger"].should be_a(Halite::Logger)
logger = client.options.features["logger"].as(Halite::Logger)
logger.writer.should be_a(Halite::Logger::Common)
client = Halite.use("logging")
client.options.features.has_key?("logging").should be_true
client.options.features["logging"].should be_a(Halite::Logging)
logger = client.options.features["logging"].as(Halite::Logging)
logger.writer.should be_a(Halite::Logging::Common)
logger.writer.skip_request_body.should be_false
logger.writer.skip_response_body.should be_false
logger.writer.skip_benchmark.should be_false
logger.writer.colorize.should be_true
end

it "sets given feature name and options" do
client = Halite.use("logger", logger: Halite::Logger::JSON.new(skip_request_body: true, colorize: false))
client.options.features.has_key?("logger").should be_true
client.options.features["logger"].should be_a(Halite::Logger)
logger = client.options.features["logger"].as(Halite::Logger)
logger.writer.should be_a(Halite::Logger::JSON)
client = Halite.use("logging", logger: Halite::Logging::JSON.new(skip_request_body: true, colorize: false))
client.options.features.has_key?("logging").should be_true
client.options.features["logging"].should be_a(Halite::Logging)
logger = client.options.features["logging"].as(Halite::Logging)
logger.writer.should be_a(Halite::Logging::JSON)
logger.writer.skip_request_body.should be_true
logger.writer.skip_response_body.should be_false
logger.writer.skip_benchmark.should be_false
Expand Down
16 changes: 8 additions & 8 deletions src/halite/chainable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ module Halite
branch(default_options)
end

# Returns `Options` self with given the logger which it integration from `Halite::Logger`.
# Returns `Options` self with given the logger which it integration from `Halite::Logging`.
#
# #### Simple logging
#
Expand Down Expand Up @@ -260,11 +260,11 @@ module Halite
#
# #### Use custom logger
#
# Creating the custom logger by integration `Halite::Logger::Abstract` abstract class.
# Creating the custom logger by integration `Halite::Logging::Abstract` abstract class.
# Here has two methods must be implement: `#request` and `#response`.
#
# ```
# class CustomLogger < Halite::Logger::Abstract
# class CustomLogger < Halite::Logging::Abstract
# def request(request)
# @logger.info "| >> | %s | %s %s" % [request.verb, request.uri, request.body]
# end
Expand All @@ -275,7 +275,7 @@ module Halite
# end
#
# # Add to adapter list (optional)
# Halite::Logger.register_adapter "custom", CustomLogger.new
# Halite::Logging.register_adapter "custom", CustomLogger.new
#
# Halite.logger(logger: CustomLogger.new)
# .get("http://httpbin.org/get", params: {name: "foobar"})
Expand All @@ -287,7 +287,7 @@ module Halite
# # => 2017-12-13 16:40:13 +08:00 | >> | GET | http://httpbin.org/get?name=foobar
# # => 2017-12-13 16:40:15 +08:00 | << | 200 | http://httpbin.org/get?name=foobar application/json
# ```
def logger(logger = Halite::Logger::Common.new)
def logger(logger = Halite::Logging::Common.new)
branch(default_options.with_logger(logger))
end

Expand Down Expand Up @@ -337,15 +337,15 @@ module Halite
# #### Use json logger
#
# ```
# Halite.use("logger", format: "json")
# Halite.use("logging", format: "json")
# .get("http://httpbin.org/get", params: {name: "foobar"})
#
# # => { ... }
# ```
#
# #### Use common format logger and skip response body
# ```
# Halite.use("logger", format: "common", skip_response_body: true)
# Halite.use("logging", format: "common", skip_response_body: true)
# .get("http://httpbin.org/get", params: {name: "foobar"})
#
# # => 2018-08-28 14:58:26 +08:00 | request | GET | http://httpbin.org/get
Expand All @@ -360,7 +360,7 @@ module Halite
# Available features to review all subclasses of `Halite::Feature`.
#
# ```
# Halite.use("logger", "your-custom-feature-name")
# Halite.use("logging", "your-custom-feature-name")
# .get("http://httpbin.org/get", params: {name: "foobar"})
# ```
def use(*features)
Expand Down
18 changes: 9 additions & 9 deletions src/halite/features/logger.cr → src/halite/features/logging.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ require "colorize"
require "file_utils"

module Halite
# Logger feature
class Logger < Feature
def self.new(format : String = "common", logger : Logger::Abstract? = nil, **opts)
# Logging feature
class Logging < Feature
def self.new(format : String = "common", logger : Logging::Abstract? = nil, **opts)
return new(logger: logger) if logger
raise UnRegisterLoggerFormatError.new("Not avaiable logger format: #{format}") unless cls = Logger[format]?
raise UnRegisterLoggerFormatError.new("Not avaiable logging format: #{format}") unless cls = Logging[format]?

logger = cls.new(**opts)
new(logger: logger)
end

DEFAULT_LOGGER = Logger::Common.new
DEFAULT_LOGGER = Logging::Common.new

getter writer : Logger::Abstract
getter writer : Logging::Abstract

def initialize(**options)
@writer = options.fetch(:logger, DEFAULT_LOGGER).as(Logger::Abstract)
@writer = options.fetch(:logger, DEFAULT_LOGGER).as(Logging::Abstract)
end

def request(request)
Expand Down Expand Up @@ -93,8 +93,8 @@ module Halite

extend Register

Halite.register_feature "logger", self
Halite.register_feature "logging", self
end
end

require "./loggers/*"
require "./logging/*"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "file_utils"

class Halite::Logger
# Logger feature: Logger::Common
class Halite::Logging
# Logger feature: Logging::Common
class Common < Abstract
@request_time : Time?

Expand Down Expand Up @@ -116,6 +116,6 @@ class Halite::Logger
"#{digits.round(2).to_s}#{suffix}"
end

Logger.register "common", self
Logging.register "common", self
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "json"

class Halite::Logger
class Halite::Logging
# Logger feature: Logger::JSON
class JSON < Abstract
@created_at : Time? = nil
Expand Down Expand Up @@ -65,6 +65,6 @@ class Halite::Logger
end
end

Logger.register "json", self
Logging.register "json", self
end
end
Loading

0 comments on commit 1ec66b8

Please sign in to comment.