semantic_logger_datadog 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 601074c776382fb609d87e888c959a6d9ca99d300a651ab8ac8d68790a4d293e
4
+ data.tar.gz: 340850a4ddd2c82b54e15256b838d782c4869bcbf37974faf3e0ca8b6e654ac4
5
+ SHA512:
6
+ metadata.gz: 2544b4d572b362228616146daeaf98d9eb598cbfbb0544455c43d2059b85793f0dedbd075d425357a45010a7a433eec1d2cae8026c049ce2e821305712bee56a
7
+ data.tar.gz: 5c2ad0dbac03147d1784ccee0c3e1783fb498b4b604814fecd3600e51e535d45f942b38c5f63c8574f746ef93efd4df68636049c1d32c07aa242ef87c92939c1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 CargoSense, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # semantic_logger_datadog
2
+
3
+ **[Semantic Logger](https://logger.rocketjob.io) formatter for submitting JSON logs to [Datadog](https://www.datadoghq.com).**
4
+
5
+ [![Gem](https://img.shields.io/gem/v/semantic_logger_datadog.svg?logo=rubygems&style=for-the-badge)](https://rubygems.org/gems/semantic_logger_datadog)
6
+ [![Downloads](https://img.shields.io/gem/dt/semantic_logger_datadog.svg?logo=rubygems&style=for-the-badge)](https://rubygems.org/gems/semantic_logger_datadog)
7
+ [![Build](https://img.shields.io/github/actions/workflow/status/CargoSense/semantic_logger_datadog/ci.yml?logo=github&style=for-the-badge)](https://github.com/CargoSense/semantic_logger_datadog/actions/workflows/ci.yml)
8
+
9
+ ## Installation
10
+
11
+ Add semantic_logger_datadog to your project's `Gemfile` and run `bundle install`:
12
+
13
+ ```ruby
14
+ gem "semantic_logger_datadog"
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ require "semantic_logger_datadog"
21
+
22
+ # Set the global default log level.
23
+ SemanticLogger.default_level = :trace
24
+
25
+ # Log to a file, and use the Datadog formatter.
26
+ SemanticLogger.add_appender(file_name: "development.log", formatter: SemanticLoggerDatadog::Formatters::Json.new)
27
+
28
+ # Create an instance of a logger and add the class name to every log message.
29
+ logger = SemanticLogger["MyClass"]
30
+
31
+ # Log an info message to devleopment.log
32
+ logger.info "Hello, world!"
33
+ ```
34
+
35
+ Refer to [Semantic Logger's documentation](https://logger.rocketjob.io/customize.html) for more customization options.
36
+
37
+ ## Configuring Ruby on Rails
38
+
39
+ In addition to semantic_logger_datadog, add the [rails_semantic_logger](https://rubygems.org/gems/rails_semantic_logger) gem to your project's Gemfile:
40
+
41
+ ```ruby
42
+ gem "rails_semantic_logger"
43
+ gem "semantic_logger_datadog"
44
+ ```
45
+
46
+ Configure named tags in `config/application.rb`:
47
+
48
+ ```ruby
49
+ module App
50
+ class Application < Rails::Application
51
+ # Your app's existing configuration goes here…
52
+
53
+ # Configure named tags to add to every log message.
54
+ config.log_tags = {
55
+ remote_ip: :remote_ip,
56
+ request_id: :request_id,
57
+ url: ->(request) { request.original_url },
58
+ useragent: ->(request) { request.headers["user-agent"] },
59
+ }
60
+ end
61
+ end
62
+ ```
63
+
64
+ ### Adding custom controller action data
65
+
66
+ Optionally configure user-specific data in `app/controllers/application_controller.rb` by adding [an `append_info_to_payload` method](https://logger.rocketjob.io/rails.html#adding-custom-data-to-the-rails-completed-log-message):
67
+
68
+ ```ruby
69
+ def append_info_to_payload(payload)
70
+ super
71
+
72
+ payload[:usr] = current_user.slice(:id, :name, :email) if user_signed_in?
73
+ end
74
+ ```
75
+
76
+ > [!IMPORTANT]
77
+ > Be extremely careful when logging personally-identifiable information. If you aren't required to log this information for legal or regulatory reasons, don't! Your logs can't leak data they never stored in the first place.
78
+
79
+ ## License
80
+
81
+ semantic_logger_datadog is freely available under the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SemanticLoggerDatadog
4
+ module Formatters
5
+ class Json < Raw
6
+ def initialize(time_format: :iso_8601, time_key: :timestamp, **args)
7
+ super
8
+ end
9
+
10
+ # @return [String]
11
+ def call(*)
12
+ super.to_json
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SemanticLoggerDatadog
4
+ module Formatters
5
+ class Raw < SemanticLogger::Formatters::Raw
6
+ # @return [Hash]
7
+ def call(*)
8
+ super
9
+
10
+ request_id
11
+ dd_hash
12
+ http_hash
13
+ logger_hash
14
+ usr_hash
15
+
16
+ hash
17
+ end
18
+
19
+ # @return [Hash{Symbol => String}, nil]
20
+ def dd_hash
21
+ hash[:dd] = log.named_tags.delete(:dd)
22
+ end
23
+
24
+ # @return [String, nil]
25
+ def duration
26
+ return unless log.duration
27
+
28
+ # Convert milliseconds to nanoseconds
29
+ hash[:duration] = log.duration * 1_000_000
30
+ hash[:duration_human] = log.duration_human
31
+ end
32
+
33
+ # @return [Hash]
34
+ def exception
35
+ return unless log.exception
36
+
37
+ kind = log.exception.class.name
38
+ message = log.exception.message
39
+ stack = log.backtrace_to_s
40
+
41
+ hash.merge!(
42
+ error: { kind:, message:, stack: }.compact,
43
+ message: [kind, message].compact.join(": ")
44
+ )
45
+ end
46
+
47
+ # @return [Hash{Symbol => Hash{Symbol => String}}]
48
+ def http_hash
49
+ method, status_code = [:method, :status].map { |key| log.payload&.delete(key) }
50
+ remote_ip, url, useragent = [:remote_ip, :url, :useragent].map { |key| log.named_tags.delete(key) }
51
+
52
+ http = { method:, remote_ip:, status_code:, url:, useragent: }
53
+
54
+ http.compact!
55
+
56
+ hash[:http] = http unless http.empty?
57
+ end
58
+
59
+ # @return [Hash{Symbol => String}]
60
+ def logger_hash
61
+ hash[:logger] = {
62
+ name: logger.name,
63
+ thread_name: log.thread_name,
64
+ }
65
+ end
66
+
67
+ # Don't log process ID.
68
+ #
69
+ # @see https://logger.rocketjob.io/customize.html#example-do-not-log-the-process-id
70
+ #
71
+ # @return [nil]
72
+ def pid
73
+ end
74
+
75
+ # @return [String]
76
+ def request_id
77
+ hash[:request_id] = log.named_tags.delete(:request_id)
78
+ end
79
+
80
+ # @return [Hash{Symbol => Integer, String}, nil]
81
+ def usr_hash
82
+ hash[:usr] = log.payload&.delete(:usr)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require "rails_semantic_logger"
5
+ rescue LoadError => e
6
+ raise %(semantic_logger_datadog: Install the rails_semantic_logger gem to continue. (#{e}))
7
+ end
8
+
9
+ module SemanticLoggerDatadog
10
+ class Railtie < Rails::Railtie
11
+ initializer "semantic_logger_datadog.configure_rails_initialization" do |app|
12
+ # Set application name.
13
+ app.config.semantic_logger.application = Rails.application.name
14
+
15
+ # Set backtrace level to avoid memory leaks due to high object allocation.
16
+ #
17
+ # @see https://logger.rocketjob.io/rails.html#include-the-file-name-and-line-number-in-the-source-code-where-the-message-originated
18
+ app.config.semantic_logger.backtrace_level = :error if Rails.env.production?
19
+
20
+ # Set log output format.
21
+ #
22
+ # @see https://logger.rocketjob.io/rails.html#output-format
23
+ app.config.rails_semantic_logger.format = SemanticLoggerDatadog::Formatters::Json.new
24
+
25
+ # Log to standard output when +RAILS_LOG_TO_STDOUT+ is configured.
26
+ #
27
+ # @see https://logger.rocketjob.io/rails.html#log-to-standard-out
28
+ if ENV["RAILS_LOG_TO_STDOUT"].present?
29
+ $stdout.sync = true
30
+ app.config.rails_semantic_logger.add_file_appender = false
31
+ app.config.semantic_logger.add_appender(formatter: app.config.rails_semantic_logger.format, io: $stdout)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "semantic_logger"
4
+
5
+ require_relative "semantic_logger_datadog/formatters/raw"
6
+ require_relative "semantic_logger_datadog/formatters/json"
7
+
8
+ require_relative "semantic_logger_datadog/railtie" if defined?(Rails::Railtie)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.required_ruby_version = ">= 3.1"
5
+
6
+ spec.name = "semantic_logger_datadog"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["CargoSense"]
9
+ spec.email = ["rubygems@cargosense.com"]
10
+
11
+ spec.summary = "Semantic Logger formatter for submitting JSON logs to Datadog."
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/CargoSense/semantic_logger_datadog"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir["lib/**/*"].reject { |f| File.directory?(f) }
17
+ spec.files += ["LICENSE", "README.md"]
18
+ spec.files += ["semantic_logger_datadog.gemspec"]
19
+
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.metadata = {
23
+ "bug_tracker_uri" => "#{spec.homepage}/issues",
24
+ "changelog_uri" => "#{spec.homepage}/releases",
25
+ "documentation_uri" => "https://rubydoc.info/gems/#{spec.name}/#{spec.version}",
26
+ "homepage_uri" => spec.homepage,
27
+ "rubygems_mfa_required" => "true",
28
+ "source_code_uri" => "#{spec.homepage}/tree/v#{spec.version}",
29
+ }
30
+
31
+ spec.add_dependency "semantic_logger", "~> 4.17"
32
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semantic_logger_datadog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - CargoSense
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: semantic_logger
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '4.17'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '4.17'
26
+ description: Semantic Logger formatter for submitting JSON logs to Datadog.
27
+ email:
28
+ - rubygems@cargosense.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.md
35
+ - lib/semantic_logger_datadog.rb
36
+ - lib/semantic_logger_datadog/formatters/json.rb
37
+ - lib/semantic_logger_datadog/formatters/raw.rb
38
+ - lib/semantic_logger_datadog/railtie.rb
39
+ - semantic_logger_datadog.gemspec
40
+ homepage: https://github.com/CargoSense/semantic_logger_datadog
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ bug_tracker_uri: https://github.com/CargoSense/semantic_logger_datadog/issues
45
+ changelog_uri: https://github.com/CargoSense/semantic_logger_datadog/releases
46
+ documentation_uri: https://rubydoc.info/gems/semantic_logger_datadog/0.1.0
47
+ homepage_uri: https://github.com/CargoSense/semantic_logger_datadog
48
+ rubygems_mfa_required: 'true'
49
+ source_code_uri: https://github.com/CargoSense/semantic_logger_datadog/tree/v0.1.0
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '3.1'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.7.2
65
+ specification_version: 4
66
+ summary: Semantic Logger formatter for submitting JSON logs to Datadog.
67
+ test_files: []