Moved the majority of the macros outside of the finished block. Un-needed
This commit is contained in:
+37
-25
@@ -1,31 +1,25 @@
|
|||||||
require "log"
|
require "log"
|
||||||
|
|
||||||
module Logger
|
module Logger
|
||||||
macro included
|
|
||||||
|
# This allows the user to mark an instance variable to be kept as context when
|
||||||
|
# any ::Logger generated macro is used within and outside of class/instance methods
|
||||||
annotation LogContext
|
annotation LogContext
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Tells the ::Logger macros that if any log macros get run in a annotated method
|
||||||
|
# that it will keep track of the arguments of the method as it runs. Alongside
|
||||||
|
# providing the user the ability to exclude arguments (like when passing a
|
||||||
|
# plain text password into a method)
|
||||||
|
#
|
||||||
|
# For this you will need to make sure to use the exclude: key with an array of
|
||||||
|
# method names (strings %w[ abc ] is easiest)
|
||||||
annotation LogMethod
|
annotation LogMethod
|
||||||
end
|
end
|
||||||
|
|
||||||
annotation LogConfig
|
# Macro for doing the actual logging based on how the user has
|
||||||
end
|
# the Logger annotations
|
||||||
|
private macro log_with_context(level, message)
|
||||||
{% if @type && ! @type.has_constant?("Log") %}
|
|
||||||
Log = ::Log.for(self)
|
|
||||||
{% end %}
|
|
||||||
|
|
||||||
{% verbatim do %}
|
|
||||||
macro finished
|
|
||||||
{% defined_level_methods = [] of MacroId %}
|
|
||||||
{% levels = %i[ trace debug info notice warn error fatal ] %}
|
|
||||||
{% for level in levels %}
|
|
||||||
{% defined_level_methods << level if @type.has_method? level %}
|
|
||||||
{% end %}
|
|
||||||
{% raise "ERROR : You can't include ::Logger when you have the following defined as instance methods: #{defined_level_methods.join(", ").id}" unless defined_level_methods.empty? %}
|
|
||||||
end
|
|
||||||
|
|
||||||
macro log_with_context(level, message)
|
|
||||||
{% if @def && @def.annotation(LogMethod) %}
|
{% if @def && @def.annotation(LogMethod) %}
|
||||||
{% anno = @def.annotation(LogMethod) %}
|
{% anno = @def.annotation(LogMethod) %}
|
||||||
{% exclude = (anno[:exclude] || [] of MacroId).map(&.id) %}
|
{% exclude = (anno[:exclude] || [] of MacroId).map(&.id) %}
|
||||||
@@ -37,10 +31,9 @@ module Logger
|
|||||||
{% else %}
|
{% else %}
|
||||||
Log.{{level}} { generate_log_line {{message}} }
|
Log.{{level}} { generate_log_line {{message}} }
|
||||||
{% end %}
|
{% end %}
|
||||||
{% elsif @type %}
|
{% elsif @type && @def %}
|
||||||
{% if @type.class? %}
|
{% if @type.class? && @type.methods.includes?(@def) %}
|
||||||
{% class_def = @type.methods.includes?(@def) %}
|
{% if vars = @type.instance_vars.select(&.annotation(LogContext)) %}
|
||||||
{% if (vars = @type.instance_vars.select(&.annotation(LogContext))) && class_def %}
|
|
||||||
Log.with_context( {% for var in vars %} {{var.name}}: @{{var.name}}, {% end %} ) do
|
Log.with_context( {% for var in vars %} {{var.name}}: @{{var.name}}, {% end %} ) do
|
||||||
Log.{{level}} { generate_log_line {{message}} }
|
Log.{{level}} { generate_log_line {{message}} }
|
||||||
end
|
end
|
||||||
@@ -55,19 +48,38 @@ module Logger
|
|||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
macro generate_log_line(message)
|
# Generates the log-line (everything that isn't ::Log managed)
|
||||||
|
private macro generate_log_line(message)
|
||||||
{% if @def %}
|
{% if @def %}
|
||||||
[ "Method({{@def.name}})", {{message}}].join(" : ")
|
[ "Method({{@def.name}})", {{message}} ].join(" : ")
|
||||||
{% else %}
|
{% else %}
|
||||||
{{message}}
|
{{message}}
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
{% for level in %i[ trace debug info notice warn error fatal ].map(&.id) %}
|
{% for level in %i[ trace debug info notice warn error fatal ].map(&.id) %}
|
||||||
|
# Macro for running ::Log.{{level}} with message and splat args
|
||||||
macro {{level.downcase}}(message, *args)
|
macro {{level.downcase}}(message, *args)
|
||||||
log_with_context {{level}}, (\{{message}} \{% unless args.empty? %} % \{{args}} \{% end %})
|
log_with_context {{level}}, (\{{message}} \{% unless args.empty? %} % \{{args}} \{% end %})
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
|
macro included
|
||||||
|
{% if @type && ! @type.has_constant?("Log") %}
|
||||||
|
Log = ::Log.for(self)
|
||||||
|
{% end %}
|
||||||
|
|
||||||
|
{% verbatim do %}
|
||||||
|
macro finished
|
||||||
|
{% defined_level_methods = [] of MacroId %}
|
||||||
|
{% levels = %i[ trace debug info notice warn error fatal ] %}
|
||||||
|
{% for level in levels %}
|
||||||
|
{% defined_level_methods << level if @type.has_method? level %}
|
||||||
|
{% end %}
|
||||||
|
{% unless defined_level_methods.empty? %}
|
||||||
|
{% raise "ERROR : You can't include ::Logger when you have the following defined as instance methods: #{defined_level_methods.join(", ").id}" %}
|
||||||
|
{% end %}
|
||||||
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user