Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: logger #2067

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading