From a1ffe5bdb96788cec30b6d86f6f8aa7d8f741f1b Mon Sep 17 00:00:00 2001 From: Tasos Latsas Date: Mon, 24 May 2021 20:18:28 +0300 Subject: [PATCH] Move logger instance in the configuration object --- CHANGELOG.md | 4 ++++ README.md | 1 + lib/webpacker_uploader.rb | 7 +------ lib/webpacker_uploader/configuration.rb | 7 +++++++ lib/webpacker_uploader/instance.rb | 8 ++------ test/configuration_test.rb | 8 ++++++++ 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efad413..08901e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format ## [Unreleased] +### Changed + +- Logger is now part of the configuration object instead of a class attribute. ([#9](https://github.com/tlatsas/webpacker_uploader/pull/9), [@tlatsas](https://github.com/tlatsas)) + ## [[0.3.0]](https://github.com/tlatsas/webpacker_uploader/releases/tag/v0.3.0) - 2021-03-06 ### Added diff --git a/README.md b/README.md index 4535439..18cac11 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ WebpackerUploader currently supports the following configuration options: | option | description | default value | |----------------------|--------------------------------------------------------------|---------------------------------------| | ignored_extensions | Which files uploader should skip based on the file extension | [] | +| logger | The logger to use for logging | ActiveSupport::Logger.new(STDOUT) | | log_output | Log output as we upload files | true | | public_manifest_path | The webpack manifest path | Webpacker.config.public_manifest_path | | public_path | The webpack public output path | Webpacker.config.public_path | diff --git a/lib/webpacker_uploader.rb b/lib/webpacker_uploader.rb index 47e87c1..71555f7 100644 --- a/lib/webpacker_uploader.rb +++ b/lib/webpacker_uploader.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true require "active_support/core_ext/object/blank" -require "active_support/logger" -require "active_support/tagged_logging" module WebpackerUploader extend self @@ -20,9 +18,6 @@ def instance # @!attribute [rw] config # @see Instance#config # @!scope class - # @!attribute [rw] logger - # @see Instance.logger - # @!scope class # @!method configure # @see Instance#configure # @!scope class @@ -30,7 +25,7 @@ def instance # @return [void] # @see Instance#upload! # @!scope class - delegate :logger, :logger=, :configure, :config, :upload!, to: :instance + delegate :configure, :config, :upload!, to: :instance end require "webpacker_uploader/configuration" diff --git a/lib/webpacker_uploader/configuration.rb b/lib/webpacker_uploader/configuration.rb index 7937414..bd855b6 100644 --- a/lib/webpacker_uploader/configuration.rb +++ b/lib/webpacker_uploader/configuration.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require "active_support/logger" +require "active_support/tagged_logging" + # This is the class which holds the configuration options. # # Options are set and retrieved using `WebpackerUploader.config` @@ -8,6 +11,9 @@ class WebpackerUploader::Configuration # @return [Array] the file extentions ignored by the uploader. attr_accessor :ignored_extensions + # @return [ActiveSupport::Logger] the logger to use. + attr_accessor :logger + # @return [Boolean] whether or not to log operations. attr_accessor :log_output @@ -21,6 +27,7 @@ class WebpackerUploader::Configuration def initialize @ignored_extensions = [] + @logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) @log_output = true @public_manifest_path = ::Webpacker.config.public_manifest_path @public_path = ::Webpacker.config.public_path diff --git a/lib/webpacker_uploader/instance.rb b/lib/webpacker_uploader/instance.rb index 84d90f4..16271e9 100644 --- a/lib/webpacker_uploader/instance.rb +++ b/lib/webpacker_uploader/instance.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true class WebpackerUploader::Instance - # @!attribute [rw] logger - # @!scope class - cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) } - attr_writer :config # @private @@ -63,11 +59,11 @@ def upload!(provider, prefix: nil) file_path = config.public_path.join(path) if name.end_with?(*config.ignored_extensions) - logger.info("Skipping #{file_path}") if config.log_output? + config.logger.info("Skipping #{file_path}") if config.log_output? else content_type = WebpackerUploader::Mime.mime_type(path) - logger.info("Processing #{file_path} as #{content_type}") if config.log_output? + config.logger.info("Processing #{file_path} as #{content_type}") if config.log_output? provider.upload!(remote_path, file_path, content_type) end diff --git a/test/configuration_test.rb b/test/configuration_test.rb index ab82a03..56a53ec 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -13,6 +13,9 @@ def teardown def test_default_config_options assert_empty @config.ignored_extensions + + assert_instance_of ActiveSupport::Logger, @config.logger + assert @config.log_output assert @config.log_output? @@ -27,6 +30,9 @@ def test_changing_config_options @config.ignored_extensions = [".css", ".js"] assert_equal [".css", ".js"], @config.ignored_extensions + @config.logger = Logger.new(STDOUT) + assert_instance_of Logger, @config.logger + @config.log_output = false refute @config.log_output refute @config.log_output? @@ -41,12 +47,14 @@ def test_changing_config_options def test_configure_block WebpackerUploader.configure do |c| c.ignored_extensions = [".js"] + c.logger = Logger.new(STDOUT) c.log_output = false c.public_manifest_path = "path/to/manifest.json" c.public_path = "path/to/public/dir" end assert_equal [".js"], WebpackerUploader.config.ignored_extensions + assert_instance_of Logger, WebpackerUploader.config.logger refute WebpackerUploader.config.log_output refute WebpackerUploader.config.log_output? assert_equal "path/to/manifest.json", WebpackerUploader.config.public_manifest_path.to_s