sapience 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bbf1f419c40131407dc53eccc467636462c6627
4
- data.tar.gz: 2556a1353dfc63228882845c6515b186ef9c7fb4
3
+ metadata.gz: 11cce05eedf1219a30f6da98358d1047be347bb5
4
+ data.tar.gz: 439781b493d904279964f6b6dc74caea43058082
5
5
  SHA512:
6
- metadata.gz: 2cbd70840bd75008f69b8501dc12dd1dc4b64f7fc338beff143a2ea4bba3f9358a298e1b84d34bc32124b38a8b7f84e3f5b0ca6230a772ebb5d1a51332ecfde7
7
- data.tar.gz: 9101d5a36a37c8061eea06d930c2a55161ca2d44d75c687d7e5a9d48127dd81ceeebac358e8d0859db9887455da361adc71daac6af5111621cd877fc1e3132b0
6
+ metadata.gz: 702c8f1c1af9e49c5a2f0b3f352c4f2d0835836952e420e3e827a105b586723041605ff76ef29b1ffb1eea351a466faeabc79fa29be639e890b31b3a3581a1aa
7
+ data.tar.gz: 750f844879a9d7b5d886bc5e3fdc2a7e7aa8cf8b33487e3b090494d0b71c7b3d55b86ab2b36435abe5dbad838b8bd85a4adc028389b6b0b04afc71a78556cfb6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v1.0.3
2
+
3
+ - Rename `SAPIENCE_APP_NAME` to `APP_NAME`
4
+ - Allow for configuration inheritance (see the history for [config_loader.rb](https://github.com/reevoo/sapience-rb/blame/master/lib/sapience/config_loader.rb#L19))
5
+
1
6
  ## v1.0.2
2
7
 
3
8
  - Require `app_name` to be configured. Either set it with the environment variable `SAPIENCE_APP_NAME` or provided it when configuring the application.
data/README.md CHANGED
@@ -58,7 +58,6 @@ The `app_name` is required to be configured. Sapience will fail on startup if ap
58
58
  Sapience.configure do |config|
59
59
  config.default_level = :info
60
60
  config.backtrace_level = :error
61
- config.app_name = "my_app"
62
61
  config.appenders = [
63
62
  { stream: { io: STDOUT, formatter: :color } },
64
63
  { sentry: { dsn: "https://username:password@app.getsentry.com/00000" } },
@@ -105,6 +104,47 @@ production:
105
104
  formatter: json
106
105
  ```
107
106
 
107
+
108
+ #### Configuration Inheritance
109
+
110
+ We will use our default (or overriden - see [reevoo_sapience-rb](https://github.com/reevoo/reevoo_sapience-rb) for more info) configuration as a base. Any configuration specified inside a `config/sapience.yml` file will then me merged into the default or overridded config.
111
+
112
+ The merge will take place not at the top level but at the environment level. This means that everything inside the environment keys will be replaced with a more specific application config.
113
+
114
+ Then if a configure block is used that will take presedence.
115
+
116
+ #### App name
117
+
118
+ Sapience requires an application name to be set for your loggs and such. We decided not to guess what name you want to give your application so there will be no magic involved here. There are 3 different ways of configuring the app_name for Sapience.
119
+
120
+ ##### Environment variables
121
+
122
+ This is the preferable way. If you have many environments look into using something like dotenv locally and use the power of devops and automation for your production environments.
123
+
124
+ ```bash
125
+ APP_NAME="My Application" bundle exec rails server -p 9000
126
+ ```
127
+
128
+ ##### Configuration file
129
+
130
+ If you are in need of overriding the sapience default configuration an app_name can be used for any environment but we recommend you specify the app_name for the default section. That way you don't have to specify app_name for each environment and avoid some duplicated keys. Of course if you need to specify different app names for various environments by all means do.
131
+
132
+ ```yaml
133
+ ---
134
+ default:
135
+ app_name: My Application
136
+ ```
137
+
138
+ ##### Configuration block
139
+
140
+ This will be the top priority and is the first check. The reasoning is that if someone has taken the time to use configure with a block that should override anything set in file configuration or environment.
141
+
142
+ ```ruby
143
+ Sapience.configure do |config|
144
+ config.app_name = "My Application"
145
+ end
146
+ ```
147
+
108
148
  ## Appenders
109
149
 
110
150
  One of the things that did not suite us so well with the Semantic Logger approach was that they made a distinction between metrics and appenders. In our view anything that should potentially log something somewhere should be treated as an appender.
@@ -204,7 +244,7 @@ Formatters can be specified by using the key `formatter: :camelized_formatter_na
204
244
 
205
245
  ## Environment variables
206
246
 
207
- `SAPIENCE_APP_NAME` - If you want to provide an application name for sapience
247
+ `APP_NAME` - If you want to provide an application name for sapience it can be done here.
208
248
  `SAPIENCE_ENV` - For applications that don't use rack or rails
209
249
 
210
250
  ## Contributing
data/docker-compose.yml CHANGED
@@ -56,7 +56,7 @@ services:
56
56
  depends_on:
57
57
  - rabbitmq
58
58
  environment:
59
- SAPIENCE_APP_NAME: rails_app
59
+ APP_NAME: rails_app
60
60
  AMQP: amqp://sapience:tests@rabbitmq:5672
61
61
  PATH: /usr/src/app/bin:/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
62
62
 
@@ -69,5 +69,5 @@ services:
69
69
  entrypoint: /usr/src/app/dev-entrypoint.sh
70
70
  command: bundle exec rspec
71
71
  environment:
72
- SAPIENCE_APP_NAME: grape_app
72
+ APP_NAME: grape_app
73
73
  PATH: /usr/src/app/bin:/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
@@ -106,8 +106,8 @@ module Sapience
106
106
  end
107
107
 
108
108
  def namespace
109
- ns = Sapience.app_name
110
- ns << ".#{Sapience.environment}" if Sapience.environment
109
+ ns = Sapience.namify(Sapience.app_name)
110
+ ns << ".#{Sapience.namify(Sapience.environment)}" if Sapience.environment
111
111
  ns
112
112
  end
113
113
 
@@ -17,18 +17,28 @@ module Sapience
17
17
  DEFAULT_FILE = File.join(SAPIENCE_HOME, "config", "default.yml")
18
18
 
19
19
  def self.load_from_file
20
- file_path = config_file_path
21
- path = File.absolute_path(file_path)
22
- load_yaml_configuration(path)
20
+ default_config = load_yaml_configuration(config_file_path)
21
+ application_config = load_yaml_configuration(application_config_file)
22
+ merge_configs(default_config, application_config)
23
+ end
24
+
25
+ def self.merge_configs(left_config = {}, right_config = {})
26
+ left_config.each do |key, config = {}|
27
+ right = right_config.fetch(key) { Hash.new }
28
+ config.merge!(right)
29
+ end
30
+
31
+ (right_config.keys - left_config.keys).each do |left_over_key|
32
+ left_config[left_over_key] = right_config[left_over_key]
33
+ end
34
+ left_config
23
35
  end
24
36
 
25
37
  class << self
26
38
  private
27
39
 
28
40
  def config_file_path
29
- return application_config_file if File.exist?(application_config_file)
30
-
31
- DEFAULT_FILE
41
+ File.absolute_path(DEFAULT_FILE)
32
42
  end
33
43
 
34
44
  def application_config_file
@@ -44,6 +54,7 @@ module Sapience
44
54
  end
45
55
 
46
56
  def load_yaml_configuration(absolute_path)
57
+ return {} unless File.exist?(absolute_path)
47
58
  text = IO.read(absolute_path, encoding: "UTF-8")
48
59
  erb = ERB.new(text)
49
60
  yaml_code = erb.result
@@ -27,20 +27,40 @@ module Sapience
27
27
 
28
28
  # Logging levels in order of most detailed to most severe
29
29
  LEVELS = [:trace, :debug, :info, :warn, :error, :fatal].freeze
30
- DEFAULT_ENV = "default".freeze
31
- RACK_ENV = "RACK_ENV".freeze
32
- RAILS_ENV = "RAILS_ENV".freeze
33
- SAPIENCE_APP_NAME = "SAPIENCE_APP_NAME".freeze
34
- SAPIENCE_ENV = "SAPIENCE_ENV".freeze
30
+ APP_NAME = "APP_NAME".freeze
31
+ DEFAULT_ENV = "default".freeze
32
+ RACK_ENV = "RACK_ENV".freeze
33
+ RAILS_ENV = "RAILS_ENV".freeze
34
+ SAPIENCE_ENV = "SAPIENCE_ENV".freeze
35
+
36
+ # TODO: Maybe when configuring with a block we should create a new config?
37
+ # See the TODO note on .config for more information
38
+ def self.configure(force: false)
39
+ yield config if block_given?
40
+ return config if configured? && force == false
41
+ reload_config!
42
+ reset_appenders!
43
+ add_appenders(*config.appenders)
44
+ @@configured = true
45
+
46
+ config
47
+ end
48
+
49
+ def self.config_hash
50
+ @@config_hash ||= ConfigLoader.load_from_file
51
+ end
52
+
53
+ def self.reload_config!
54
+ @@config_hash = ConfigLoader.load_from_file
55
+ end
35
56
 
36
57
  # TODO: Should we really always read from file?
37
58
  # What if someone wants to configure sapience with a block
38
59
  # without reading the default.yml?
39
60
  def self.config
40
61
  @@config ||= begin
41
- config = ConfigLoader.load_from_file
42
- options = config[environment]
43
- options ||= default_options(config)
62
+ options = config_hash[environment]
63
+ options ||= default_options(config_hash)
44
64
  Configuration.new(options)
45
65
  end
46
66
  end
@@ -63,6 +83,7 @@ module Sapience
63
83
  @@environment = nil
64
84
  @@app_name = nil
65
85
  @@configured = false
86
+ @@config_hash = nil
66
87
  clear_tags!
67
88
  reset_appenders!
68
89
  end
@@ -89,12 +110,12 @@ module Sapience
89
110
  end
90
111
 
91
112
  def self.app_name
92
- @@app_name ||= begin
93
- appname = ENV.fetch(SAPIENCE_APP_NAME) do
94
- config.app_name
95
- end
96
- namify(appname)
97
- end
113
+ @@app_name ||= config.app_name
114
+ @@app_name ||= config_hash.fetch(environment) { {} }["app_name"]
115
+ @@app_name ||= config_hash.fetch(DEFAULT_ENV) { {} }["app_name"]
116
+ @@app_name ||= ENV[APP_NAME]
117
+ fail AppNameMissing, "app_name is not configured. See documentation for more information" unless @@app_name
118
+ @@app_name
98
119
  end
99
120
 
100
121
  def self.namify(appname, sep = "_")
@@ -113,21 +134,6 @@ module Sapience
113
134
  end
114
135
  appname.downcase
115
136
  end
116
- private_class_method :namify
117
-
118
-
119
- # TODO: Maybe when configuring with a block we should create a new config?
120
- # See the TODO note on .config for more information
121
- def self.configure(force: false)
122
- yield config if block_given?
123
- fail AppNameMissing, "app_name is not configured. See documentation for more information" unless app_name
124
- return config if configured? && force == false
125
- reset_appenders!
126
- add_appenders(*config.appenders)
127
- @@configured = true
128
-
129
- config
130
- end
131
137
 
132
138
  # Return a logger for the supplied class or class_name
133
139
  def self.[](klass)
@@ -1,3 +1,3 @@
1
1
  module Sapience
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../..
3
3
  specs:
4
- sapience (1.0.1)
4
+ sapience (1.0.2)
5
5
  concurrent-ruby (~> 1.0)
6
6
 
7
7
  GEM
@@ -94,7 +94,7 @@ GEM
94
94
  rspec-support (~> 3.5.0)
95
95
  rspec-support (3.5.0)
96
96
  ruby-progressbar (1.8.1)
97
- sentry-raven (1.2.3)
97
+ sentry-raven (2.0.1)
98
98
  faraday (>= 0.7.6, < 0.10.x)
99
99
  simplecov (0.12.0)
100
100
  docile (~> 1.1.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../..
3
3
  specs:
4
- sapience (1.0.1)
4
+ sapience (1.0.2)
5
5
  concurrent-ruby (~> 1.0)
6
6
 
7
7
  GEM
@@ -93,7 +93,7 @@ GEM
93
93
  rspec-support (~> 3.5.0)
94
94
  rspec-support (3.5.0)
95
95
  ruby-progressbar (1.8.1)
96
- sentry-raven (1.2.3)
96
+ sentry-raven (2.0.1)
97
97
  faraday (>= 0.7.6, < 0.10.x)
98
98
  simplecov (0.12.0)
99
99
  docile (~> 1.1.0)
@@ -28,9 +28,6 @@ RSpec.configure do |config|
28
28
  config.filter_rails_from_backtrace!
29
29
  config.before(:each) do
30
30
  Sapience.reset!
31
- end
32
-
33
- config.before(:each) do
34
31
  FileUtils.cp(
35
32
  Rails.root.join("spec/fixtures/sapience.yml"),
36
33
  Rails.root.join("config/sapience.yml"),
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sapience
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-09-26 00:00:00.000000000 Z
12
+ date: 2016-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby