|
| 1 | +require "logger" |
| 2 | +require "debug_logging/version" |
| 3 | +require "debug_logging/argument_printer" |
| 4 | +require "debug_logging/instance_logger_modulizer" |
| 5 | +require "debug_logging/instance_logger" |
| 6 | +require "debug_logging/class_logger" |
| 7 | + |
| 8 | +#################### |
| 9 | +# # |
| 10 | +# Next Level Magic # |
| 11 | +# Classes inheriting from Module. |
| 12 | +# Cats and dogs sleeping together. |
| 13 | +# # |
| 14 | +#################### |
| 15 | +# # |
| 16 | +# NOTE: The manner this is made to work for class methods is totally different |
| 17 | +# than the way this is made to work for instance methods. |
| 18 | +# NOTE: The instance method manner works on Ruby 2.0+ |
| 19 | +# NOTE: The class method manner works on Ruby 2.1+ |
| 20 | +# # |
| 21 | +#################### |
| 22 | +# # |
| 23 | +# USAGE (see specs)# |
| 24 | +# # |
| 25 | +# class Car |
| 26 | +# |
| 27 | +# # For instance methods: |
| 28 | +# # Option 1: specify the exact method(s) to add logging to |
| 29 | +# include DebugLogging::InstanceLogger.new(i_methods: [:drive, :stop]) |
| 30 | +# |
| 31 | +# extend DebugLogging::ClassLogger |
| 32 | +# |
| 33 | +# logged def self.make; new; end |
| 34 | +# def self.design(*args); new; end |
| 35 | +# def self.safety(*args); new; end |
| 36 | +# logged :design, :safety |
| 37 | +# |
| 38 | +# def drive(speed); speed; end |
| 39 | +# def stop; 0; end |
| 40 | +# |
| 41 | +# # For instance methods: |
| 42 | +# # Option 2: add logging to all instance methods defined above (but *not* defined below) |
| 43 | +# include DebugLogging::InstanceLogger.new(i_methods: self.instance_methods(false)) |
| 44 | +# |
| 45 | +# def will_not_be_logged; false; end |
| 46 | +# |
| 47 | +# end |
| 48 | +# # |
| 49 | +#################### |
| 50 | + |
| 51 | +module DebugLogging |
| 52 | + def self.config_reset |
| 53 | + @@logger = nil |
| 54 | + @@log_level = :debug |
| 55 | + @@last_hash_to_s_proc = nil |
| 56 | + @@last_hash_max_length = 1_000 |
| 57 | + @@args_max_length = 1_000 |
| 58 | + @@instance_benchmarks = false |
| 59 | + @@class_benchmarks = false |
| 60 | + @@add_invocation_id = true |
| 61 | + @@ellipsis = " ✂️ …".freeze |
| 62 | + end |
| 63 | + def self.logger |
| 64 | + @@logger ||= Logger.new(STDOUT) |
| 65 | + end |
| 66 | + def self.logger=(logger) |
| 67 | + @@logger = logger |
| 68 | + end |
| 69 | + def self.log_level |
| 70 | + @@log_level || :debug |
| 71 | + end |
| 72 | + def self.log_level=(log_level) |
| 73 | + @@log_level = log_level |
| 74 | + end |
| 75 | + def self.last_hash_to_s_proc |
| 76 | + @@last_hash_to_s_proc |
| 77 | + end |
| 78 | + def self.last_hash_to_s_proc=(last_hash_to_s_proc) |
| 79 | + @@last_hash_to_s_proc = last_hash_to_s_proc |
| 80 | + end |
| 81 | + def self.last_hash_max_length |
| 82 | + @@last_hash_max_length || 1_000 |
| 83 | + end |
| 84 | + def self.last_hash_max_length=(last_hash_max_length) |
| 85 | + @@last_hash_max_length = last_hash_max_length |
| 86 | + end |
| 87 | + def self.args_max_length |
| 88 | + @@args_max_length || 1_000 |
| 89 | + end |
| 90 | + def self.args_max_length=(args_max_length) |
| 91 | + @@args_max_length = args_max_length |
| 92 | + end |
| 93 | + def self.instance_benchmarks |
| 94 | + @@instance_benchmarks |
| 95 | + end |
| 96 | + def self.instance_benchmarks=(instance_benchmarks) |
| 97 | + require "benchmark" if instance_benchmarks |
| 98 | + @@instance_benchmarks = instance_benchmarks |
| 99 | + end |
| 100 | + def self.class_benchmarks |
| 101 | + @@class_benchmarks |
| 102 | + end |
| 103 | + def self.class_benchmarks=(class_benchmarks) |
| 104 | + require "benchmark" if class_benchmarks |
| 105 | + @@class_benchmarks = class_benchmarks |
| 106 | + end |
| 107 | + def self.add_invocation_id |
| 108 | + @@add_invocation_id |
| 109 | + end |
| 110 | + def self.add_invocation_id=(add_invocation_id) |
| 111 | + @@add_invocation_id = add_invocation_id |
| 112 | + end |
| 113 | + def self.ellipsis |
| 114 | + @@ellipsis |
| 115 | + end |
| 116 | + def self.ellipsis=(ellipsis) |
| 117 | + @@ellipsis = ellipsis |
| 118 | + end |
| 119 | + def self.log(message) |
| 120 | + logger.send(log_level, message) |
| 121 | + end |
| 122 | + config_reset # setup defaults |
| 123 | +end |
0 commit comments