-
-
Notifications
You must be signed in to change notification settings - Fork 521
Add new Sentry.logger #2620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Sentry.logger #2620
Changes from 20 commits
6d00939
570b449
4d808cc
8ce961e
b8ef85e
3aba1f5
4d83fac
5645fb4
4d5571e
4c7c762
02da067
8bc6add
9658adf
633a135
d074fac
bdf604d
13863bc
d879a8b
7a96c56
c41de52
8514a25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,10 @@ module Sentry | |
| class LogEvent < Event | ||
| TYPE = "log" | ||
|
|
||
| DEFAULT_PARAMETERS = [].freeze | ||
| DEFAULT_ATTRIBUTES = {}.freeze | ||
| DEFAULT_CONTEXT = {}.freeze | ||
|
|
||
| SERIALIZEABLE_ATTRIBUTES = %i[ | ||
| level | ||
| body | ||
|
|
@@ -21,20 +25,23 @@ class LogEvent < Event | |
| "sentry.release" => :release, | ||
| "sentry.address" => :server_name, | ||
| "sentry.sdk.name" => :sdk_name, | ||
| "sentry.sdk.version" => :sdk_version | ||
| "sentry.sdk.version" => :sdk_version, | ||
| "sentry.message.template" => :template | ||
| } | ||
|
|
||
| LEVELS = %i[trace debug info warn error fatal].freeze | ||
|
|
||
| attr_accessor :level, :body, :attributes, :trace_id | ||
| attr_accessor :level, :body, :template, :attributes | ||
|
|
||
| def initialize(configuration: Sentry.configuration, **options) | ||
| super(configuration: configuration) | ||
|
|
||
| @type = TYPE | ||
| @level = options.fetch(:level) | ||
| @body = options[:body] | ||
| @attributes = options[:attributes] || {} | ||
| @contexts = {} | ||
| @template = @body if is_template? | ||
| @attributes = options[:attributes] || DEFAULT_ATTRIBUTES | ||
| @contexts = DEFAULT_CONTEXT | ||
| end | ||
|
|
||
| def to_hash | ||
|
|
@@ -72,15 +79,25 @@ def serialize_timestamp | |
| end | ||
|
|
||
| def serialize_trace_id | ||
| @contexts.dig(:trace, :trace_id) | ||
| contexts.dig(:trace, :trace_id) | ||
| end | ||
|
|
||
| def serialize_parent_span_id | ||
| @contexts.dig(:trace, :parent_span_id) | ||
| contexts.dig(:trace, :parent_span_id) | ||
| end | ||
|
|
||
| def serialize_body | ||
| if parameters.empty? | ||
| body | ||
| elsif parameters.is_a?(Hash) | ||
| body % parameters | ||
| else | ||
| sprintf(body, *parameters) | ||
| end | ||
| end | ||
|
|
||
| def serialize_attributes | ||
| hash = @attributes.each_with_object({}) do |(key, value), memo| | ||
| hash = attributes.each_with_object({}) do |(key, value), memo| | ||
| memo[key] = attribute_hash(value) | ||
| end | ||
|
|
||
|
|
@@ -109,5 +126,34 @@ def value_type(value) | |
| "string" | ||
| end | ||
| end | ||
|
|
||
| def parameters | ||
| @parameters ||= begin | ||
| return DEFAULT_PARAMETERS unless template | ||
|
|
||
| parameters = template_tokens.empty? ? | ||
| attributes.fetch(:parameters, DEFAULT_PARAMETERS) : attributes.slice(*template_tokens) | ||
|
|
||
| if parameters.is_a?(Hash) | ||
| parameters.each do |key, value| | ||
| attributes["sentry.message.parameters.#{key}"] = value | ||
| end | ||
| else | ||
| parameters.each_with_index do |param, index| | ||
| attributes["sentry.message.parameters.#{index}"] = param | ||
|
||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| TOKEN_REGEXP = /%\{(\w+)\}/ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should have a clear note in docs that our templates are expected to be like "This is a template with a %{var}"and not what ruby people are used to "This is a template with a #{var}" |
||
|
|
||
| def template_tokens | ||
| @template_tokens ||= body.scan(TOKEN_REGEXP).flatten.map(&:to_sym) | ||
| end | ||
|
|
||
| def is_template? | ||
| body.include?("%s") || TOKEN_REGEXP.match?(body) | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.