Class: Makit::Logging::Configuration
- Inherits:
-
Object
- Object
- Makit::Logging::Configuration
- Defined in:
- lib/makit/logging/configuration.rb
Overview
Configuration management for unified logging
Provides schema validation, default configurations, and helper methods for setting up the unified file sink with various output configurations.
Instance Attribute Summary collapse
-
#configurations ⇒ Array<Hash>
readonly
List of output configurations.
Class Method Summary collapse
-
.ci_config ⇒ Configuration
Get CI/CD configuration.
-
.default_config ⇒ Configuration
Get default configuration.
-
.development_config ⇒ Configuration
Get development configuration.
-
.for_environment(environment) ⇒ Configuration
Create environment-specific configuration.
-
.from_json(json_file) ⇒ Configuration
Create configuration from JSON file.
-
.from_yaml(yaml_file) ⇒ Configuration
Create configuration from YAML file.
-
.production_config ⇒ Configuration
Get production configuration.
-
.test_config ⇒ Configuration
Get test configuration.
Instance Method Summary collapse
-
#initialize(configurations: []) ⇒ Configuration
constructor
Initialize configuration.
-
#save_json(json_file) ⇒ void
Save configuration to JSON file.
-
#save_yaml(yaml_file) ⇒ void
Save configuration to YAML file.
-
#to_hash ⇒ Hash
Get configuration as hash.
-
#valid? ⇒ Boolean
Check if configuration is valid.
-
#validate! ⇒ Boolean
Validate configuration.
Constructor Details
#initialize(configurations: []) ⇒ Configuration
Initialize configuration
30 31 32 |
# File 'lib/makit/logging/configuration.rb', line 30 def initialize(configurations: []) @configurations = configurations end |
Instance Attribute Details
#configurations ⇒ Array<Hash> (readonly)
Returns list of output configurations.
25 26 27 |
# File 'lib/makit/logging/configuration.rb', line 25 def configurations @configurations end |
Class Method Details
.ci_config ⇒ Configuration
Get CI/CD configuration
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/makit/logging/configuration.rb', line 170 def self.ci_config new( configurations: [ { file: $stdout, format: :console, show_level: true, }, { file: "artifacts/build.log", format: :json, append: true, include_metadata: true, }, ], ) end |
.default_config ⇒ Configuration
Get default configuration
90 91 92 93 94 95 96 97 |
# File 'lib/makit/logging/configuration.rb', line 90 def self.default_config new( configurations: [ { file: $stdout, format: :console }, { file: "artifacts/makit.log", format: :json, append: true }, ], ) end |
.development_config ⇒ Configuration
Get development configuration
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/makit/logging/configuration.rb', line 102 def self.development_config new( configurations: [ { file: $stdout, format: :console, show_timestamp: true, show_level: true, }, { file: "logs/development.log", format: :text, append: true, include_context: true, }, { file: "logs/debug.log", format: :json, append: true, min_level: :debug, }, ], ) end |
.for_environment(environment) ⇒ Configuration
Create environment-specific configuration
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/makit/logging/configuration.rb', line 72 def self.for_environment(environment) case environment when :development development_config when :production production_config when :test test_config when :ci ci_config else default_config end end |
.from_json(json_file) ⇒ Configuration
Create configuration from JSON file
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/makit/logging/configuration.rb', line 55 def self.from_json(json_file) require "json" data = JSON.parse(File.read(json_file)) configurations = data["logging"]["sinks"] || [] # Convert string keys to symbols configurations = configurations.map do |config| config.transform_keys(&:to_sym) end new(configurations: configurations) end |
.from_yaml(yaml_file) ⇒ Configuration
Create configuration from YAML file
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/makit/logging/configuration.rb', line 38 def self.from_yaml(yaml_file) require "yaml" data = YAML.load_file(yaml_file) configurations = data["logging"]["sinks"] || [] # Convert string keys to symbols configurations = configurations.map do |config| config.transform_keys(&:to_sym) end new(configurations: configurations) end |
.production_config ⇒ Configuration
Get production configuration
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/makit/logging/configuration.rb', line 130 def self.production_config new( configurations: [ { file: "logs/application.log", format: :json, append: true, include_metadata: true, rotation: { max_size: "100MB", max_files: 10 }, }, { file: "logs/errors.log", format: :json, append: true, min_level: :error, include_metadata: true, }, ], ) end |
.test_config ⇒ Configuration
Get test configuration
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/makit/logging/configuration.rb', line 154 def self.test_config new( configurations: [ { file: "logs/test.log", format: :text, append: false, include_context: true, }, ], ) end |
Instance Method Details
#save_json(json_file) ⇒ void
This method returns an undefined value.
Save configuration to JSON file
238 239 240 241 242 |
# File 'lib/makit/logging/configuration.rb', line 238 def save_json(json_file) require "json" FileUtils.mkdir_p(File.dirname(json_file)) File.write(json_file, JSON.pretty_generate(to_hash)) end |
#save_yaml(yaml_file) ⇒ void
This method returns an undefined value.
Save configuration to YAML file
228 229 230 231 232 |
# File 'lib/makit/logging/configuration.rb', line 228 def save_yaml(yaml_file) require "yaml" FileUtils.mkdir_p(File.dirname(yaml_file)) File.write(yaml_file, to_hash.to_yaml) end |
#to_hash ⇒ Hash
Get configuration as hash
216 217 218 219 220 221 222 |
# File 'lib/makit/logging/configuration.rb', line 216 def to_hash { logging: { sinks: @configurations, }, } end |
#valid? ⇒ Boolean
Check if configuration is valid
206 207 208 209 210 211 |
# File 'lib/makit/logging/configuration.rb', line 206 def valid? validate! true rescue ArgumentError false end |
#validate! ⇒ Boolean
Validate configuration
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/makit/logging/configuration.rb', line 192 def validate! raise ArgumentError, "configurations must be an array" unless @configurations.is_a?(Array) raise ArgumentError, "at least one configuration is required" if @configurations.empty? @configurations.each_with_index do |config, index| validate_single_configuration(config, index) end true end |