google-logging-utils 0.a → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8520762ae422e08bbfdc9444cc5ebf43d56fd60f164de2be543a89183943fe97
4
- data.tar.gz: fa6b93ece4bf65d9121fc5b3c8611a707df776f117cc346e6dd5f7e8f2c57311
3
+ metadata.gz: 1c4b0a1e9ee0a457ab30b53029ee805fbf8e5e56a4eea90eb3fa26278d8c7f31
4
+ data.tar.gz: 0a3047afc45fe59ed38054aede68b59ae941fa87f83b2faa723d93b2e60b5cfb
5
5
  SHA512:
6
- metadata.gz: 779b4fc40fc1d78d0384d8dbc3f827953740d9ce1ba6843bc1a42a79976809b86cebad05539d2456440c9298758a1c3deb26482a9ce48ba4cccf848daab2bd42
7
- data.tar.gz: 2472d45c358a9512f42e0be7479a2e045d0e25ddda3f0a2cbca80e29f82c87523cae3f65185c6e6ff73e2161cb690d83d7cace7f9e326aa0cd015d421da8567a
6
+ metadata.gz: fb7b3148bad01f1097af24ea861801d81697bfe56ad69887a8913dbf5d8b5a7ee15e8ae466bbb16cfc27cbebb8e1ab3d1c472f38aacb439740c143ddb9f0d734
7
+ data.tar.gz: bf476028d0fd941b0d466d94c46ffdee4ef05b8b9b161eca87152b2e009ccd41278b515a0bbb0570ccd1c4261ba49f9dc0d05c90915f586003cc7e67cf8dc8d0
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ ## Changelog
2
+
3
+ ### 0.2.0 (2025-04-30)
4
+
5
+ #### Features
6
+
7
+ * Updated required Ruby version to 3.1 ([#29](https://github.com/googleapis/ruby-core-libraries/issues/29))
8
+
9
+ ### 0.1.0 (2024-08-27)
10
+
11
+ #### Features
12
+
13
+ * Initial release with Google log message class and structured formatter ([#11](https://github.com/googleapis/ruby-core-libraries/issues/11))
@@ -0,0 +1,43 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project,
4
+ and in the interest of fostering an open and welcoming community,
5
+ we pledge to respect all people who contribute through reporting issues,
6
+ posting feature requests, updating documentation,
7
+ submitting pull requests or patches, and other activities.
8
+
9
+ We are committed to making participation in this project
10
+ a harassment-free experience for everyone,
11
+ regardless of level of experience, gender, gender identity and expression,
12
+ sexual orientation, disability, personal appearance,
13
+ body size, race, ethnicity, age, religion, or nationality.
14
+
15
+ Examples of unacceptable behavior by participants include:
16
+
17
+ * The use of sexualized language or imagery
18
+ * Personal attacks
19
+ * Trolling or insulting/derogatory comments
20
+ * Public or private harassment
21
+ * Publishing other's private information,
22
+ such as physical or electronic
23
+ addresses, without explicit permission
24
+ * Other unethical or unprofessional conduct.
25
+
26
+ Project maintainers have the right and responsibility to remove, edit, or reject
27
+ comments, commits, code, wiki edits, issues, and other contributions
28
+ that are not aligned to this Code of Conduct.
29
+ By adopting this Code of Conduct,
30
+ project maintainers commit themselves to fairly and consistently
31
+ applying these principles to every aspect of managing this project.
32
+ Project maintainers who do not follow or enforce the Code of Conduct
33
+ may be permanently removed from the project team.
34
+
35
+ This code of conduct applies both within project spaces and in public spaces
36
+ when an individual is representing the project or its community.
37
+
38
+ Instances of abusive, harassing, or otherwise unacceptable behavior
39
+ may be reported by opening an issue
40
+ or contacting one or more of the project maintainers.
41
+
42
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0,
43
+ available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,28 @@
1
+ # How to Contribute
2
+
3
+ We'd love to accept your patches and contributions to this project. There are
4
+ just a few small guidelines you need to follow.
5
+
6
+ ## Contributor License Agreement
7
+
8
+ Contributions to this project must be accompanied by a Contributor License
9
+ Agreement. You (or your employer) retain the copyright to your contribution;
10
+ this simply gives us permission to use and redistribute your contributions as
11
+ part of the project. Head over to <https://cla.developers.google.com/> to see
12
+ your current agreements on file or to sign a new one.
13
+
14
+ You generally only need to submit a CLA once, so if you've already submitted one
15
+ (even if it was for a different project), you probably don't need to do it
16
+ again.
17
+
18
+ ## Code reviews
19
+
20
+ All submissions, including submissions by project members, require review. We
21
+ use GitHub pull requests for this purpose. Consult
22
+ [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
23
+ information on using pull requests.
24
+
25
+ ## Community Guidelines
26
+
27
+ This project follows [Google's Open Source Community
28
+ Guidelines](https://opensource.google.com/conduct/).
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md CHANGED
@@ -1,8 +1,29 @@
1
- # Placeholder for Ruby gem google-logging-utils
2
-
3
- This is a placeholder for the future Google-authored gem google-logging-utils.
4
- This placeholder is being released on 2024-08-27 in order to reserve the name.
5
- The final gem should be available shortly after that date. If it has not been
6
- released in a timely manner, or if this placeholder interferes with your work,
7
- you can contact the Google Ruby team by opening an issue in the GitHub
8
- repository https://github.com/googleapis/google-cloud-ruby.
1
+ # Logging utilities for Google Cloud
2
+
3
+ Utility classes for logging to Google Cloud Logging.
4
+
5
+ ## Supported Ruby Versions
6
+
7
+ This library is supported on Ruby 3.0+.
8
+
9
+ Google provides official support for Ruby versions that are actively supported
10
+ by Ruby Core—that is, Ruby versions that are either in normal maintenance or in
11
+ security maintenance, and not end of life. Currently, this means Ruby 3.0 and
12
+ later. Older versions of Ruby _may_ still work, but are unsupported and not
13
+ recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
14
+ about the Ruby support schedule.
15
+
16
+ ## Contributing
17
+
18
+ Contributions to this library are always welcome and highly encouraged.
19
+
20
+ See the {file:CONTRIBUTING.md CONTRIBUTING} documentation for more information on how to get started.
21
+
22
+ ## Versioning
23
+
24
+ This library is currently a **preview** with no guarantees of stability or support. Please get
25
+ involved and let us know if you find it useful and we'll work towards a stable version.
26
+
27
+ ## Disclaimer
28
+
29
+ This is not an official Google product.
@@ -0,0 +1,318 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2024 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "json"
18
+ require "google/logging/source_location"
19
+
20
+ module Google
21
+ module Logging
22
+ ##
23
+ # A log message that can be formatted either as a normal text log entry or
24
+ # as a structured log entry suitable for Google Cloud Logging.
25
+ #
26
+ # A log message has a "body" which consists of either a string message, a
27
+ # JSON object (i.e. a Hash whose values are typically strings or numbers
28
+ # but could be nested arrays and hashes), or both. It also includes several
29
+ # additional optional fields used by the Google Cloud Logging backend.
30
+ #
31
+ # Most log formatters will render the message body as a string and ignore
32
+ # the other attributes. The {StructuredFormatter}, however, will format the
33
+ # full message data in the JSON format understood by the Google logging
34
+ # agent.
35
+ #
36
+ class Message
37
+ ##
38
+ # Create a log message from an input object, which can be any of:
39
+ #
40
+ # * An existing Message object.
41
+ # * A Hash. Symbol keys are used as keyword arguments to the
42
+ # {Message#initialize} constructor. String keys are treated as fields
43
+ # in the JSON payload.
44
+ # * Any other object is converted to a string with `to_s` and used as a
45
+ # simple text payload.
46
+ #
47
+ # @param input [Object] A log message input object.
48
+ # @return [Message]
49
+ #
50
+ def self.from input
51
+ case input
52
+ when Hash
53
+ kwargs = {}
54
+ fields = nil
55
+ input.each do |k, v|
56
+ if k.is_a? Symbol
57
+ kwargs[k] = v
58
+ else
59
+ (fields ||= {})[k.to_s] = v
60
+ end
61
+ end
62
+ if kwargs[:fields] && fields
63
+ kwargs[:fields].merge! fields
64
+ else
65
+ kwargs[:fields] ||= fields
66
+ end
67
+ new(**kwargs)
68
+ when Message
69
+ input
70
+ else
71
+ new message: input.to_s
72
+ end
73
+ end
74
+
75
+ ##
76
+ # Low-level constructor for a logging message.
77
+ # All arguments are optional, with the exception that at least one of
78
+ # `:message` and `:fields` must be provided.
79
+ #
80
+ # @param message [String] The main log message as a string.
81
+ # @param fields [Hash{String=>Object}] The log message as a set of
82
+ # key-value pairs representing a JSON object.
83
+ # @param timestamp [Time,Numeric,:now] The timestamp for the log entry.
84
+ # Can be provided as a Time object, a Numeric indicating the seconds
85
+ # since epoch, or `:now` to use the current time. Optional.
86
+ # @param source_location [SourceLocation,Hash,:caller,nil] The source
87
+ # location for the log entry. Can be provided as a {SourceLocation}
88
+ # object, a Hash containing exactly the keys `:file`, `:line`, and
89
+ # `:function`, or `:caller` to use the location of the caller.
90
+ # Optional.
91
+ # @param insert_id [String] A unique ID for this log entry that could be
92
+ # used on the backend to dedup messages. Optional.
93
+ # @param trace [String] A Google Cloud Trace resource name. Optional.
94
+ # @param span_id [String] The trace span containing this entry. Optional.
95
+ # @param trace_sampled [boolean] Whether this trace is sampled. Optional.
96
+ # @param labels [Hash{String=>String}] Optional metadata.
97
+ #
98
+ def initialize message: nil,
99
+ fields: nil,
100
+ timestamp: nil,
101
+ source_location: nil,
102
+ insert_id: nil,
103
+ trace: nil,
104
+ span_id: nil,
105
+ trace_sampled: nil,
106
+ labels: nil
107
+ @fields = interpret_fields fields
108
+ @message, @full_message = interpret_message message, @fields
109
+ @timestamp = interpret_timestamp timestamp
110
+ @source_location = interpret_source_location source_location
111
+ @insert_id = interpret_string insert_id
112
+ @trace = interpret_string trace
113
+ @span_id = interpret_string span_id
114
+ @trace_sampled = interpret_boolean trace_sampled
115
+ @labels = interpret_labels labels
116
+ end
117
+
118
+ ##
119
+ # @return [String] The message as a string. This is always present as a
120
+ # nonempty string, and can be reliably used as the "display" of this
121
+ # log entry in a list of entries.
122
+ #
123
+ attr_reader :message
124
+ alias to_s message
125
+
126
+ ##
127
+ # @return [String] A full string representation of the message and fields
128
+ # as rendered in the standard logger formatter.
129
+ #
130
+ attr_reader :full_message
131
+ alias inspect full_message
132
+
133
+ ##
134
+ # @return [Hash{String=>Object},nil] The log message as a set of
135
+ # key-value pairs, or nil if not present.
136
+ #
137
+ attr_reader :fields
138
+
139
+ ##
140
+ # @return [Time,nil] The timestamp for the log entry, or nil if not
141
+ # present.
142
+ #
143
+ attr_reader :timestamp
144
+
145
+ ##
146
+ # @return [SourceLocation,nil] The source location for the log entry, or
147
+ # nil if not present.
148
+ #
149
+ attr_reader :source_location
150
+
151
+ ##
152
+ # @return [String,nil] The unique ID for this log entry that could be
153
+ # used on the backend to dedup messages, or nil if not present.
154
+ #
155
+ attr_reader :insert_id
156
+
157
+ ##
158
+ # @return [String,nil] The Google Cloud Trace resource name, or nil if
159
+ # not present.
160
+ #
161
+ attr_reader :trace
162
+
163
+ ##
164
+ # @return [String,nil] The trace span containing this entry, or nil if
165
+ # not present.
166
+ #
167
+ attr_reader :span_id
168
+
169
+ ##
170
+ # @return [true,false,nil] Whether this trace is sampled, or nil if not
171
+ # present or known.
172
+ #
173
+ attr_reader :trace_sampled
174
+ alias trace_sampled? trace_sampled
175
+
176
+ ##
177
+ # @return [Hash{String=>String},nil] Metadata, or nil if not present.
178
+ #
179
+ attr_reader :labels
180
+
181
+ ##
182
+ # @return [Hash] A hash of kwargs that can be passed to the constructor
183
+ # to clone this message.
184
+ #
185
+ def to_h
186
+ {
187
+ message: @message,
188
+ fields: @fields.dup,
189
+ timestamp: @timestamp,
190
+ source_location: @source_location,
191
+ insert_id: @insert_id,
192
+ trace: @trace,
193
+ span_id: @span_id,
194
+ trace_sampled: @trace_sampled,
195
+ labels: @labels.dup
196
+ }
197
+ end
198
+
199
+ # @private
200
+ def == other
201
+ return false unless other.is_a? Message
202
+ message == other.message &&
203
+ fields == other.fields &&
204
+ timestamp == other.timestamp &&
205
+ source_location == other.source_location &&
206
+ insert_id == other.insert_id &&
207
+ trace == other.trace &&
208
+ span_id == other.span_id &&
209
+ trace_sampled? == other.trace_sampled? &&
210
+ labels == other.labels
211
+ end
212
+ alias eql? ==
213
+
214
+ # @private
215
+ def hash
216
+ [message, fields, timestamp, source_location, insert_id, trace, span_id, trace_sampled, labels].hash
217
+ end
218
+
219
+ private
220
+
221
+ # @private
222
+ DISALLOWED_FIELDS = [
223
+ "severity",
224
+ "message",
225
+ "log",
226
+ "httpRequest",
227
+ "timestamp"
228
+ ].freeze
229
+ private_constant :DISALLOWED_FIELDS
230
+
231
+ def interpret_fields fields
232
+ return nil if fields.nil?
233
+ fields = normalize_json fields
234
+ fields.each_key do |key|
235
+ if DISALLOWED_FIELDS.include?(key) || key.start_with?("logging.googleapis.com/")
236
+ raise ArgumentError, "Field key not allowed: #{key}"
237
+ end
238
+ end
239
+ fields
240
+ end
241
+
242
+ def normalize_json input
243
+ case input
244
+ when Hash
245
+ input.to_h { |k, v| [k.to_s, normalize_json(v)] }
246
+ when Array
247
+ input.map { |v| normalize_json v }
248
+ when Integer, Float, nil, true, false
249
+ input
250
+ when Rational
251
+ if input.denominator == 1
252
+ input.numerator
253
+ else
254
+ input.to_f
255
+ end
256
+ else
257
+ input.to_s
258
+ end
259
+ end
260
+
261
+ def interpret_message message, fields
262
+ if message
263
+ message = message.to_s
264
+ full_message = fields ? "#{message} -- #{JSON.generate fields}" : message
265
+ [message, full_message]
266
+ else
267
+ fields_json = JSON.generate fields
268
+ [fields_json, fields_json]
269
+ end
270
+ end
271
+
272
+ def interpret_timestamp timestamp
273
+ case timestamp
274
+ when Time
275
+ timestamp
276
+ when Numeric
277
+ Time.at timestamp
278
+ when :now
279
+ Time.now.utc
280
+ end
281
+ end
282
+
283
+ def interpret_source_location source_location
284
+ case source_location
285
+ when SourceLocation
286
+ source_location
287
+ when Hash
288
+ SourceLocation.new(**source_location)
289
+ when :caller
290
+ SourceLocation.for_caller omit_files: [__FILE__]
291
+ end
292
+ end
293
+
294
+ def interpret_string input
295
+ input&.to_s
296
+ end
297
+
298
+ def interpret_boolean input
299
+ return nil if input.nil?
300
+ input ? true : false
301
+ end
302
+
303
+ def interpret_labels input
304
+ return nil if input.nil?
305
+ input.to_h do |k, v|
306
+ v_converted =
307
+ case v
308
+ when Array, Hash
309
+ JSON.generate v
310
+ else
311
+ v.to_s
312
+ end
313
+ [k.to_s, v_converted]
314
+ end
315
+ end
316
+ end
317
+ end
318
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2024 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ module Google
19
+ module Logging
20
+ ##
21
+ # An object representing a source location as used by Google Logging.
22
+ #
23
+ class SourceLocation
24
+ ##
25
+ # Returns a SourceLocation corresponding to the caller.
26
+ # This basically walks the stack trace backwards until it finds the
27
+ # first entry not in the `google/logging/message.rb` source file or in
28
+ # any of the other files optionally listed.
29
+ #
30
+ # @param locations [Array<Thread::Backtrace::Location>] The caller stack
31
+ # to search. Optional; defaults to the current stack.
32
+ # @param extra_depth [Integer] Optional extra steps backwards to walk.
33
+ # Defaults to 0.
34
+ # @param omit_files [Array<String,Regexp>] File paths to omit.
35
+ # @return [SourceLocation,nil] The SourceLocation, or nil if none found.
36
+ #
37
+ def self.for_caller locations: nil, extra_depth: 0, omit_files: nil
38
+ in_file = true
39
+ omit_files = [__FILE__] + Array(omit_files)
40
+ (locations || self.caller_locations).each do |loc|
41
+ if in_file
42
+ next if omit_files.any? { |pat| pat === loc.absolute_path }
43
+ in_file = false
44
+ end
45
+ extra_depth -= 1
46
+ next unless extra_depth.negative?
47
+ return new file: loc.path, line: loc.lineno.to_s, function: loc.base_label
48
+ end
49
+ nil
50
+ end
51
+
52
+ ##
53
+ # Low-level constructor.
54
+ #
55
+ # @param file [String] Path to the source file.
56
+ # @param line [String] Line number as a string.
57
+ # @param function [String] Name of the calling function.
58
+ #
59
+ def initialize file:, line:, function:
60
+ @file = file.to_s
61
+ @line = line.to_s
62
+ @function = function.to_s
63
+ end
64
+
65
+ # @return [String] Path to the source file.
66
+ attr_reader :file
67
+
68
+ # @return [String] Line number as a string.
69
+ attr_reader :line
70
+
71
+ # @return [String] Name of the calling function.
72
+ attr_reader :function
73
+
74
+ # @private
75
+ def to_h
76
+ {
77
+ file: @file,
78
+ line: @line,
79
+ function: @function
80
+ }
81
+ end
82
+
83
+ # @private
84
+ def == other
85
+ return false unless other.is_a? SourceLocation
86
+ file == other.file && line == other.line && function == other.function
87
+ end
88
+ alias eql? ==
89
+
90
+ # @private
91
+ def hash
92
+ [file, line, function].hash
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2024 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "json"
18
+ require "google/logging/message"
19
+
20
+ module Google
21
+ module Logging
22
+ ##
23
+ # A log formatter that outputs the JSON-based structured logging format
24
+ # (https://cloud.google.com/logging/docs/structured-logging) understood by
25
+ # Google's logging agent.
26
+ #
27
+ class StructuredFormatter
28
+ # @private
29
+ CLOUD_SEVERITY = Hash.new { |_h, k| k }.merge(
30
+ "WARN" => "WARNING",
31
+ "FATAL" => "CRITICAL",
32
+ "ANY" => "DEFAULT"
33
+ ).freeze
34
+
35
+ # @private
36
+ def call severity, time, progname, msg
37
+ msg = Message.from msg
38
+ time = msg.timestamp || time
39
+ struct = {
40
+ "severity" => CLOUD_SEVERITY[severity],
41
+ "message" => msg.message,
42
+ "timestamp" => {
43
+ "seconds" => time.to_i,
44
+ "nanos" => time.nsec
45
+ }
46
+ }
47
+ struct.merge! msg.fields if msg.fields
48
+ struct["logging.googleapis.com/sourceLocation"] = msg.source_location.to_h if msg.source_location
49
+ struct["logging.googleapis.com/insertId"] = msg.insert_id if msg.insert_id
50
+ struct["logging.googleapis.com/spanId"] = msg.span_id if msg.span_id
51
+ struct["logging.googleapis.com/trace"] = msg.trace if msg.trace
52
+ struct["logging.googleapis.com/traceSampled"] = msg.trace_sampled if msg.trace_sampled
53
+ struct["logging.googleapis.com/labels"] = msg.labels if msg.labels
54
+ struct["progname"] ||= progname if progname
55
+ content = JSON.generate struct
56
+ "#{content}\n"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2024 Google LLC
2
4
  #
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
4
6
  # you may not use this file except in compliance with the License.
5
7
  # You may obtain a copy of the License at
6
8
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
9
+ # https://www.apache.org/licenses/LICENSE-2.0
8
10
  #
9
11
  # Unless required by applicable law or agreed to in writing, software
10
12
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -12,10 +14,12 @@
12
14
  # See the License for the specific language governing permissions and
13
15
  # limitations under the License.
14
16
 
17
+
15
18
  module Google
16
19
  module Logging
17
20
  module Utils
18
- VERSION = "0.a"
21
+ # Library version
22
+ VERSION = "0.2.0"
19
23
  end
20
24
  end
21
25
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2024 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "google/logging/utils/version"
18
+ require "google/logging/message"
19
+ require "google/logging/source_location"
20
+ require "google/logging/structured_formatter"
21
+
22
+ ##
23
+ # Google products namespace
24
+ #
25
+ module Google
26
+ ##
27
+ # Classes and utilities related to logging to and from Google products
28
+ #
29
+ module Logging
30
+ ##
31
+ # Logging utilities namespace
32
+ #
33
+ module Utils
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2024 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "google/logging/utils"
metadata CHANGED
@@ -1,33 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-logging-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.a
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Google LLC
8
- autorequire:
7
+ - Google API Authors
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-08-27 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
- description: Placeholder for the future Google-authored gem google-logging-utils.
14
- This placeholder is being released on 2024-08-27 in order to reserve the name. The
15
- final gem should be available shortly after that date. If it has not been released
16
- in a timely manner, or if this placeholder interferes with your work, you can contact
17
- the Google Ruby team by opening an issue in the GitHub repository https://github.com/googleapis/google-cloud-ruby.
18
- email: googleapis-packages@google.com
12
+ email:
13
+ - googleapis-packages@google.com
19
14
  executables: []
20
15
  extensions: []
21
16
  extra_rdoc_files: []
22
17
  files:
23
- - LICENSE.md
18
+ - CHANGELOG.md
19
+ - CODE_OF_CONDUCT.md
20
+ - CONTRIBUTING.md
21
+ - LICENSE
24
22
  - README.md
23
+ - lib/google-logging-utils.rb
24
+ - lib/google/logging/message.rb
25
+ - lib/google/logging/source_location.rb
26
+ - lib/google/logging/structured_formatter.rb
27
+ - lib/google/logging/utils.rb
25
28
  - lib/google/logging/utils/version.rb
26
- homepage: https://github.com/googleapis/google-cloud-ruby
29
+ homepage: https://github.com/googleapis/ruby-core-libraries
27
30
  licenses:
28
31
  - Apache-2.0
29
32
  metadata: {}
30
- post_install_message:
31
33
  rdoc_options: []
32
34
  require_paths:
33
35
  - lib
@@ -35,15 +37,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
35
37
  requirements:
36
38
  - - ">="
37
39
  - !ruby/object:Gem::Version
38
- version: '3.0'
40
+ version: '3.1'
39
41
  required_rubygems_version: !ruby/object:Gem::Requirement
40
42
  requirements:
41
43
  - - ">="
42
44
  - !ruby/object:Gem::Version
43
45
  version: '0'
44
46
  requirements: []
45
- rubygems_version: 3.5.6
46
- signing_key:
47
+ rubygems_version: 3.6.8
47
48
  specification_version: 4
48
- summary: Placeholder for the future Google-authored gem google-logging-utils
49
+ summary: Utility classes for logging to Google Cloud Logging
49
50
  test_files: []
data/LICENSE.md DELETED
@@ -1,201 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the purposes
44
- of this License, Derivative Works shall not include works that remain
45
- separable from, or merely link (or bind by name) to the interfaces of,
46
- the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including
49
- the original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright owner
52
- or by an individual or Legal Entity authorized to submit on behalf of
53
- the copyright owner. For the purposes of this definition, "submitted"
54
- means any form of electronic, verbal, or written communication sent
55
- to the Licensor or its representatives, including but not limited to
56
- communication on electronic mailing lists, source code control systems,
57
- and issue tracking systems that are managed by, or on behalf of, the
58
- Licensor for the purpose of discussing and improving the Work, but
59
- excluding communication that is conspicuously marked or otherwise
60
- designated in writing by the copyright owner as "Not a Contribution."
61
-
62
- "Contributor" shall mean Licensor and any individual or Legal Entity
63
- on behalf of whom a Contribution has been received by Licensor and
64
- subsequently incorporated within the Work.
65
-
66
- 2. Grant of Copyright License. Subject to the terms and conditions of
67
- this License, each Contributor hereby grants to You a perpetual,
68
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
- copyright license to reproduce, prepare Derivative Works of,
70
- publicly display, publicly perform, sublicense, and distribute the
71
- Work and such Derivative Works in Source or Object form.
72
-
73
- 3. Grant of Patent License. Subject to the terms and conditions of
74
- this License, each Contributor hereby grants to You a perpetual,
75
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
- (except as stated in this section) patent license to make, have made,
77
- use, offer to sell, sell, import, and otherwise transfer the Work,
78
- where such license applies only to those patent claims licensable
79
- by such Contributor that are necessarily infringed by their
80
- Contribution(s) alone or by combination of their Contribution(s)
81
- with the Work to which such Contribution(s) was submitted. If You
82
- institute patent litigation against any entity (including a
83
- cross-claim or counterclaim in a lawsuit) alleging that the Work
84
- or a Contribution incorporated within the Work constitutes direct
85
- or contributory patent infringement, then any patent licenses
86
- granted to You under this License for that Work shall terminate
87
- as of the date such litigation is filed.
88
-
89
- 4. Redistribution. You may reproduce and distribute copies of the
90
- Work or Derivative Works thereof in any medium, with or without
91
- modifications, and in Source or Object form, provided that You
92
- meet the following conditions:
93
-
94
- (a) You must give any other recipients of the Work or
95
- Derivative Works a copy of this License; and
96
-
97
- (b) You must cause any modified files to carry prominent notices
98
- stating that You changed the files; and
99
-
100
- (c) You must retain, in the Source form of any Derivative Works
101
- that You distribute, all copyright, patent, trademark, and
102
- attribution notices from the Source form of the Work,
103
- excluding those notices that do not pertain to any part of
104
- the Derivative Works; and
105
-
106
- (d) If the Work includes a "NOTICE" text file as part of its
107
- distribution, then any Derivative Works that You distribute must
108
- include a readable copy of the attribution notices contained
109
- within such NOTICE file, excluding those notices that do not
110
- pertain to any part of the Derivative Works, in at least one
111
- of the following places: within a NOTICE text file distributed
112
- as part of the Derivative Works; within the Source form or
113
- documentation, if provided along with the Derivative Works; or,
114
- within a display generated by the Derivative Works, if and
115
- wherever such third-party notices normally appear. The contents
116
- of the NOTICE file are for informational purposes only and
117
- do not modify the License. You may add Your own attribution
118
- notices within Derivative Works that You distribute, alongside
119
- or as an addendum to the NOTICE text from the Work, provided
120
- that such additional attribution notices cannot be construed
121
- as modifying the License.
122
-
123
- You may add Your own copyright statement to Your modifications and
124
- may provide additional or different license terms and conditions
125
- for use, reproduction, or distribution of Your modifications, or
126
- for any such Derivative Works as a whole, provided Your use,
127
- reproduction, and distribution of the Work otherwise complies with
128
- the conditions stated in this License.
129
-
130
- 5. Submission of Contributions. Unless You explicitly state otherwise,
131
- any Contribution intentionally submitted for inclusion in the Work
132
- by You to the Licensor shall be under the terms and conditions of
133
- this License, without any additional terms or conditions.
134
- Notwithstanding the above, nothing herein shall supersede or modify
135
- the terms of any separate license agreement you may have executed
136
- with Licensor regarding such Contributions.
137
-
138
- 6. Trademarks. This License does not grant permission to use the trade
139
- names, trademarks, service marks, or product names of the Licensor,
140
- except as required for reasonable and customary use in describing the
141
- origin of the Work and reproducing the content of the NOTICE file.
142
-
143
- 7. Disclaimer of Warranty. Unless required by applicable law or
144
- agreed to in writing, Licensor provides the Work (and each
145
- Contributor provides its Contributions) on an "AS IS" BASIS,
146
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
- implied, including, without limitation, any warranties or conditions
148
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
- PARTICULAR PURPOSE. You are solely responsible for determining the
150
- appropriateness of using or redistributing the Work and assume any
151
- risks associated with Your exercise of permissions under this License.
152
-
153
- 8. Limitation of Liability. In no event and under no legal theory,
154
- whether in tort (including negligence), contract, or otherwise,
155
- unless required by applicable law (such as deliberate and grossly
156
- negligent acts) or agreed to in writing, shall any Contributor be
157
- liable to You for damages, including any direct, indirect, special,
158
- incidental, or consequential damages of any character arising as a
159
- result of this License or out of the use or inability to use the
160
- Work (including but not limited to damages for loss of goodwill,
161
- work stoppage, computer failure or malfunction, or any and all
162
- other commercial damages or losses), even if such Contributor
163
- has been advised of the possibility of such damages.
164
-
165
- 9. Accepting Warranty or Additional Liability. While redistributing
166
- the Work or Derivative Works thereof, You may choose to offer,
167
- and charge a fee for, acceptance of support, warranty, indemnity,
168
- or other liability obligations and/or rights consistent with this
169
- License. However, in accepting such obligations, You may act only
170
- on Your own behalf and on Your sole responsibility, not on behalf
171
- of any other Contributor, and only if You agree to indemnify,
172
- defend, and hold each Contributor harmless for any liability
173
- incurred by, or claims asserted against, such Contributor by reason
174
- of your accepting any such warranty or additional liability.
175
-
176
- END OF TERMS AND CONDITIONS
177
-
178
- APPENDIX: How to apply the Apache License to your work.
179
-
180
- To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "[]"
182
- replaced with your own identifying information. (Don't include
183
- the brackets!) The text should be enclosed in the appropriate
184
- comment syntax for the file format. We also recommend that a
185
- file or class name and description of purpose be included on the
186
- same "printed page" as the copyright notice for easier
187
- identification within third-party archives.
188
-
189
- Copyright [yyyy] [name of copyright owner]
190
-
191
- Licensed under the Apache License, Version 2.0 (the "License");
192
- you may not use this file except in compliance with the License.
193
- You may obtain a copy of the License at
194
-
195
- http://www.apache.org/licenses/LICENSE-2.0
196
-
197
- Unless required by applicable law or agreed to in writing, software
198
- distributed under the License is distributed on an "AS IS" BASIS,
199
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
- See the License for the specific language governing permissions and
201
- limitations under the License.