Skip to content

Commit

Permalink
feature: logger (#2067)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob authored Dec 4, 2023
1 parent 3cc41a3 commit f6d29c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
23 changes: 2 additions & 21 deletions lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class << self
delegate :license, :app, :error_manager, :tool_manager, :resource_manager, to: Avo::Current

def boot
boot_logger
boot_fields
@logger = Avo.configuration.logger
@field_manager = Avo::Fields::FieldManager.build
@cache_store = Avo.configuration.cache_store
plugin_manager.boot_plugins
Avo.run_load_hooks(:boot, self)
Expand Down Expand Up @@ -124,25 +124,6 @@ def mount_engines
mount Avo::Pro::Engine, at: "/avo-pro" if defined?(Avo::Pro::Engine)
}
end

private

def boot_logger
file_logger = ActiveSupport::Logger.new(Rails.root.join("log", "avo.log"))

file_logger.datetime_format = "%Y-%m-%d %H:%M:%S"
file_logger.formatter = proc do |severity, time, progname, msg|
"[Avo] #{time}: #{msg}\n".tap do |i|
puts i
end
end

@logger = file_logger
end

def boot_fields
@field_manager = Avo::Fields::FieldManager.build
end
end
end

Expand Down
21 changes: 21 additions & 0 deletions lib/avo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Configuration
attr_writer :branding
attr_writer :root_path
attr_writer :cache_store
attr_writer :logger
attr_accessor :timezone
attr_accessor :per_page
attr_accessor :per_page_steps
Expand Down Expand Up @@ -97,6 +98,7 @@ def initialize
@resource_parent_controller = "Avo::ResourcesController"
@mount_avo_engines = true
@cache_store = computed_cache_store
@logger = default_logger
end

def current_user_method(&block)
Expand Down Expand Up @@ -193,6 +195,25 @@ def computed_cache_store
end
}
end

def logger
Avo::ExecutionContext.new(target: @logger).handle
end

def default_logger
-> {
file_logger = ActiveSupport::Logger.new(Rails.root.join("log", "avo.log"))

file_logger.datetime_format = "%Y-%m-%d %H:%M:%S"
file_logger.formatter = proc do |severity, time, progname, msg|
"[Avo] #{time}: #{msg}\n".tap do |i|
puts i
end
end

file_logger
}
end
end

def self.configuration
Expand Down
14 changes: 14 additions & 0 deletions lib/generators/avo/templates/initializer/avo.tt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ Avo.configure do |config|
## provide a lambda to enable or disable cache_resource_filters per user/resource.
# config.cache_resource_filters = -> { current_user.cache_resource_filters? }

## == Logger ==
# config.logger = -> {
# file_logger = ActiveSupport::Logger.new(Rails.root.join("log", "avo.log"))
#
# file_logger.datetime_format = "%Y-%m-%d %H:%M:%S"
# file_logger.formatter = proc do |severity, time, progname, msg|
# "[Avo] #{time}: #{msg}\n".tap do |i|
# puts i
# end
# end
#
# file_logger
# }

## == Customization ==
# config.app_name = 'Avocadelicious'
# config.timezone = 'UTC'
Expand Down

0 comments on commit f6d29c1

Please sign in to comment.