google-cloud-logging 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -167,7 +167,7 @@ module Google
167
167
  # @private Raise an error unless an active connection to the service
168
168
  # is available.
169
169
  def ensure_service!
170
- fail "Must have active connection to service" unless @service
170
+ raise "Must have active connection to service" unless @service
171
171
  end
172
172
  end
173
173
  end
@@ -28,8 +28,7 @@ module Google
28
28
  class Operation
29
29
  ##
30
30
  # @private Create an empty Operation object.
31
- def initialize
32
- end
31
+ def initialize; end
33
32
 
34
33
  ##
35
34
  # An arbitrary operation identifier. Log entries with the same
@@ -28,8 +28,7 @@ module Google
28
28
  class SourceLocation
29
29
  ##
30
30
  # @private Create an empty SourceLocation object.
31
- def initialize
32
- end
31
+ def initialize; end
33
32
 
34
33
  ##
35
34
  # Source file name. Depending on the runtime environment, this might
@@ -146,7 +146,7 @@ module Google
146
146
  # @private Raise an error unless an active connection to the service
147
147
  # is available.
148
148
  def ensure_service!
149
- fail "Must have active connection to service" unless @service
149
+ raise "Must have active connection to service" unless @service
150
150
  end
151
151
  end
152
152
  end
@@ -71,7 +71,7 @@ module Google
71
71
  ##
72
72
  # The Google Cloud log_name to write the log entry with.
73
73
  attr_reader :log_name
74
- alias_method :progname, :log_name
74
+ alias progname log_name
75
75
 
76
76
  ##
77
77
  # The Google Cloud resource to write the log entry with.
@@ -84,8 +84,8 @@ module Google
84
84
  ##
85
85
  # The logging severity threshold (e.g. `Logger::INFO`)
86
86
  attr_reader :level
87
- alias_method :sev_threshold, :level
88
- alias_method :local_level, :level
87
+ alias sev_threshold level
88
+ alias local_level level
89
89
 
90
90
  ##
91
91
  # Boolean flag that indicates whether this logger can be silenced or
@@ -317,7 +317,7 @@ module Google
317
317
  write_entry severity, message unless @closed
318
318
  true
319
319
  end
320
- alias_method :log, :add
320
+ alias log add
321
321
 
322
322
  ##
323
323
  # Logs the given message at UNKNOWN severity.
@@ -393,11 +393,13 @@ module Google
393
393
  #
394
394
  def level= severity
395
395
  new_level = derive_severity severity
396
- fail ArgumentError, "invalid log level: #{severity}" if new_level.nil?
396
+ if new_level.nil?
397
+ raise ArgumentError, "invalid log level: #{severity}"
398
+ end
397
399
  @level = new_level
398
400
  end
399
- alias_method :sev_threshold=, :level=
400
- alias_method :local_level=, :level=
401
+ alias sev_threshold= level=
402
+ alias local_level= level=
401
403
 
402
404
  ##
403
405
  # Close the logging "device". This effectively disables logging from
@@ -482,7 +484,7 @@ module Google
482
484
 
483
485
  ##
484
486
  # @deprecated Use delete_request_info
485
- alias_method :delete_trace_id, :delete_request_info
487
+ alias delete_trace_id delete_request_info
486
488
 
487
489
  ##
488
490
  # No-op method. Created to match the spec of ActiveSupport::Logger#flush
@@ -584,15 +586,14 @@ module Google
584
586
  when "error".freeze then ::Logger::ERROR
585
587
  when "fatal".freeze then ::Logger::FATAL
586
588
  when "unknown".freeze then ::Logger::UNKNOWN
587
- else nil
588
589
  end
589
590
  end
590
591
 
591
592
  ##
592
593
  # @private Get Google Cloud deverity from logger level number.
593
594
  def gcloud_severity severity_int
594
- %i(DEBUG INFO WARNING ERROR CRITICAL DEFAULT)[severity_int]
595
- rescue
595
+ %i[DEBUG INFO WARNING ERROR CRITICAL DEFAULT][severity_int]
596
+ rescue StandardError
596
597
  :DEFAULT
597
598
  end
598
599
 
@@ -125,7 +125,7 @@ module Google
125
125
  @grpc = service.get_metric name
126
126
  true
127
127
  end
128
- alias_method :refresh!, :reload!
128
+ alias refresh! reload!
129
129
 
130
130
  ##
131
131
  # Permanently deletes the logs-based metric.
@@ -160,7 +160,7 @@ module Google
160
160
  # @private Raise an error unless an active connection to the service is
161
161
  # available.
162
162
  def ensure_service!
163
- fail "Must have active connection to service" unless service
163
+ raise "Must have active connection to service" unless service
164
164
  end
165
165
  end
166
166
  end
@@ -161,7 +161,7 @@ module Google
161
161
  # @private Raise an error unless an active connection to the service
162
162
  # is available.
163
163
  def ensure_service!
164
- fail "Must have active connection to service" unless @service
164
+ raise "Must have active connection to service" unless @service
165
165
  end
166
166
  end
167
167
  end
@@ -22,12 +22,13 @@ module Google
22
22
  ##
23
23
  # The default log name used to instantiate the default logger if one
24
24
  # isn't provided.
25
- DEFAULT_LOG_NAME = "ruby_app_log"
25
+ DEFAULT_LOG_NAME = "ruby_app_log".freeze
26
26
 
27
27
  ##
28
28
  # A default value for the log_name_map argument. Directs health check
29
29
  # logs to a separate log name so they don't spam the main log.
30
- DEFAULT_LOG_NAME_MAP = { "/_ah/health" => "ruby_health_check_log" }
30
+ DEFAULT_LOG_NAME_MAP =
31
+ { "/_ah/health" => "ruby_health_check_log" }.freeze
31
32
 
32
33
  ##
33
34
  # The Google::Cloud::Logging::Logger instance
@@ -59,11 +60,12 @@ module Google
59
60
  @logger = logger
60
61
  else
61
62
  log_name = configuration.log_name
62
- logging = Logging.new project: configuration.project_id,
63
- keyfile: configuration.keyfile
63
+ logging = Logging.new project_id: configuration.project_id,
64
+ credentials: configuration.credentials
64
65
  resource = Middleware.build_monitored_resource(
65
66
  configuration.monitored_resource.type,
66
- configuration.monitored_resource.labels)
67
+ configuration.monitored_resource.labels
68
+ )
67
69
  @logger = logging.logger log_name, resource
68
70
  end
69
71
  end
@@ -225,7 +227,8 @@ module Google
225
227
  if Google::Cloud.env.app_engine?
226
228
  ["gae_app", {
227
229
  module_id: Google::Cloud.env.app_engine_service_id,
228
- version_id: Google::Cloud.env.app_engine_service_version }]
230
+ version_id: Google::Cloud.env.app_engine_service_version
231
+ }]
229
232
  elsif Google::Cloud.env.container_engine?
230
233
  ["container", {
231
234
  cluster_name: Google::Cloud.env.container_engine_cluster_name,
@@ -235,7 +238,8 @@ module Google
235
238
  elsif Google::Cloud.env.compute_engine?
236
239
  ["gce_instance", {
237
240
  instance_id: Google::Cloud.env.instance_name,
238
- zone: Google::Cloud.env.instance_zone }]
241
+ zone: Google::Cloud.env.instance_zone
242
+ }]
239
243
  else
240
244
  ["global", {}]
241
245
  end
@@ -256,12 +260,14 @@ module Google
256
260
  # already.
257
261
  #
258
262
  def load_config **kwargs
259
- configuration.project_id = kwargs[:project_id] ||
260
- configuration.project_id
261
- configuration.keyfile = kwargs[:keyfile] ||
262
- configuration.keyfile
263
- configuration.log_name_map ||= kwargs[:log_name_map] ||
264
- configuration.log_name_map
263
+ project_id = kwargs[:project] || kwargs[:project_id]
264
+ configuration.project_id = project_id unless project_id.nil?
265
+
266
+ creds = kwargs[:credentials] || kwargs[:keyfile]
267
+ configuration.credentials = creds unless creds.nil?
268
+
269
+ log_name_map = kwargs[:log_name_map]
270
+ configuration.log_name_map = log_name_map unless log_name_map.nil?
265
271
 
266
272
  init_default_config
267
273
  end
@@ -269,9 +275,10 @@ module Google
269
275
  ##
270
276
  # Fallback to default configuration values if not defined already
271
277
  def init_default_config
272
- configuration.project_id ||= Cloud.configure.project_id ||
273
- Logging::Project.default_project_id
274
- configuration.keyfile ||= Cloud.configure.keyfile
278
+ configuration.project_id ||= begin
279
+ (Cloud.configure.project_id || Logging.default_project_id)
280
+ end
281
+ configuration.credentials ||= Cloud.configure.credentials
275
282
  configuration.log_name ||= DEFAULT_LOG_NAME
276
283
  configuration.log_name_map ||= DEFAULT_LOG_NAME_MAP
277
284
  end
@@ -14,7 +14,6 @@
14
14
 
15
15
 
16
16
  require "google/cloud/errors"
17
- require "google/cloud/env"
18
17
  require "google/cloud/logging/service"
19
18
  require "google/cloud/logging/credentials"
20
19
  require "google/cloud/logging/log/list"
@@ -75,16 +74,7 @@ module Google
75
74
  def project_id
76
75
  service.project
77
76
  end
78
- alias_method :project, :project_id
79
-
80
- ##
81
- # @private Default project.
82
- def self.default_project_id
83
- ENV["LOGGING_PROJECT"] ||
84
- ENV["GOOGLE_CLOUD_PROJECT"] ||
85
- ENV["GCLOUD_PROJECT"] ||
86
- Google::Cloud.env.project_id
87
- end
77
+ alias project project_id
88
78
 
89
79
  ##
90
80
  # Lists log entries. Use this method to retrieve log entries from Cloud
@@ -164,7 +154,7 @@ module Google
164
154
  filter: filter, order: order,
165
155
  projects: projects
166
156
  end
167
- alias_method :find_entries, :entries
157
+ alias find_entries entries
168
158
 
169
159
  ##
170
160
  # Creates an new Entry instance that may be populated and written to the
@@ -226,7 +216,7 @@ module Google
226
216
  e.payload = payload if payload
227
217
  e
228
218
  end
229
- alias_method :new_entry, :entry
219
+ alias new_entry entry
230
220
 
231
221
  ##
232
222
  # Writes log entries to the Stackdriver Logging service.
@@ -469,9 +459,9 @@ module Google
469
459
  max: max
470
460
  Log::List.from_grpc list_grpc, service, resource: resource, max: max
471
461
  end
472
- alias_method :find_logs, :logs
473
- alias_method :log_names, :logs
474
- alias_method :find_log_names, :logs
462
+ alias find_logs logs
463
+ alias log_names logs
464
+ alias find_log_names logs
475
465
 
476
466
  ##
477
467
  # Deletes a log and all its log entries. The log will reappear if it
@@ -537,7 +527,7 @@ module Google
537
527
  list_grpc = service.list_resource_descriptors token: token, max: max
538
528
  ResourceDescriptor::List.from_grpc list_grpc, service, max
539
529
  end
540
- alias_method :find_resource_descriptors, :resource_descriptors
530
+ alias find_resource_descriptors resource_descriptors
541
531
 
542
532
  ##
543
533
  # Creates a new monitored resource instance.
@@ -564,7 +554,7 @@ module Google
564
554
  r.labels = labels
565
555
  end
566
556
  end
567
- alias_method :new_resource, :resource
557
+ alias new_resource resource
568
558
 
569
559
  ##
570
560
  # Retrieves the list of sinks belonging to the project.
@@ -600,7 +590,10 @@ module Google
600
590
  list_grpc = service.list_sinks token: token, max: max
601
591
  Sink::List.from_grpc list_grpc, service, max
602
592
  end
603
- alias_method :find_sinks, :sinks
593
+ alias find_sinks sinks
594
+
595
+ # rubocop:disable Metrics/LineLength
596
+ # overload is too long...
604
597
 
605
598
  ##
606
599
  # Creates a new project sink. When you create a sink, only new log
@@ -621,40 +614,27 @@ module Google
621
614
  # @see https://cloud.google.com/logging/docs/export/configure_export#setting_product_name_short_permissions_for_writing_exported_logs
622
615
  # Permissions for writing exported logs
623
616
  #
624
- # @param [String] name The client-assigned sink identifier. Sink
625
- # identifiers are limited to 1000 characters and can include only the
626
- # following characters: `A-Z`, `a-z`, `0-9`, and the special
627
- # characters `_-.`.
628
- # @param [String] destination The resource name of the export
629
- # destination. See [About
630
- # sinks](https://cloud.google.com/logging/docs/api/tasks/exporting-logs#about_sinks)
631
- # for examples.
632
- # @param [String, nil] filter An [advanced logs
633
- # filter](https://cloud.google.com/logging/docs/view/advanced_filters)
634
- # that defines the log entries to be exported. The filter must be
635
- # consistent with the log entry format designed by the `version`
636
- # parameter, regardless of the format of the log entry that was
637
- # originally written to Stackdriver Logging.
638
- # @param [Time, nil] start_at The time at which this sink will begin
639
- # exporting log entries. If this value is present, then log entries
640
- # are exported only if `start_at` is less than the log entry's
641
- # timestamp. Optional.
642
- # @param [Time, nil] end_at Time at which this sink will stop exporting
643
- # log entries. If this value is present, then log entries are exported
644
- # only if the log entry's timestamp is less than `end_at`. Optional.
645
- # @param [Symbol] version The log entry version used when exporting log
646
- # entries from this sink. This version does not have to correspond to
647
- # the version of the log entry when it was written to Stackdriver
648
- # Logging. Accepted values are `:unspecified`, `:v2`, and `:v1`.
649
- # Version 2 is currently the preferred format. An unspecified version
650
- # format currently defaults to V2 in the service. The default value is
651
- # `:unspecified`.
652
- # @param [Boolean] unique_writer_identity Whether the sink will have a
653
- # dedicated service account returned in the sink's `writer_identity`.
654
- # Set this field to be true to export logs from one project to a
655
- # different project. This field is ignored for non-project sinks
656
- # (e.g. organization sinks) because those sinks are required to have
657
- # dedicated service accounts. Optional.
617
+ # @overload create_sink(name, destination, filter: nil, unique_writer_identity: nil)
618
+ # @param [String] name The client-assigned sink identifier. Sink
619
+ # identifiers are limited to 1000 characters and can include only
620
+ # the following characters: `A-Z`, `a-z`, `0-9`, and the special
621
+ # characters `_-.`.
622
+ # @param [String] destination The resource name of the export
623
+ # destination. See [About
624
+ # sinks](https://cloud.google.com/logging/docs/api/tasks/exporting-logs#about_sinks)
625
+ # for examples.
626
+ # @param [String, nil] filter An [advanced logs
627
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters)
628
+ # that defines the log entries to be exported. The filter must be
629
+ # consistent with the log entry format designed by the `version`
630
+ # parameter, regardless of the format of the log entry that was
631
+ # originally written to Stackdriver Logging.
632
+ # @param [Boolean] unique_writer_identity Whether the sink will have a
633
+ # dedicated service account returned in the sink's
634
+ # `writer_identity`. Set this field to be true to export logs from
635
+ # one project to a different project. This field is ignored for
636
+ # non-project sinks (e.g. organization sinks) because those sinks
637
+ # are required to have dedicated service accounts. Optional.
658
638
  #
659
639
  # @return [Google::Cloud::Logging::Sink] a project sink
660
640
  #
@@ -676,18 +656,29 @@ module Google
676
656
  # sink = logging.create_sink "my-sink",
677
657
  # "storage.googleapis.com/#{bucket.id}"
678
658
  #
679
- def create_sink name, destination, filter: nil, start_at: nil,
680
- end_at: nil, version: :unspecified,
681
- unique_writer_identity: nil
682
- version = Sink.resolve_version version
659
+ def create_sink name, destination, filter: nil,
660
+ unique_writer_identity: nil,
661
+ start_at: nil, end_at: nil, version: nil
683
662
  ensure_service!
663
+
664
+ if start_at
665
+ warn "[DEPRECATION] start_at is deprecated and will be ignored."
666
+ end
667
+ if end_at
668
+ warn "[DEPRECATION] end_at is deprecated and will be ignored."
669
+ end
670
+ if version
671
+ warn "[DEPRECATION] version is deprecated and will be ignored."
672
+ end
673
+
684
674
  grpc = service.create_sink \
685
- name, destination, filter, version,
686
- start_time: start_at, end_time: end_at,
675
+ name, destination, filter,
687
676
  unique_writer_identity: unique_writer_identity
688
677
  Sink.from_grpc grpc, service
689
678
  end
690
- alias_method :new_sink, :create_sink
679
+ alias new_sink create_sink
680
+
681
+ # rubocop:enable Metrics/LineLength
691
682
 
692
683
  ##
693
684
  # Retrieves a sink by name.
@@ -716,8 +707,8 @@ module Google
716
707
  rescue Google::Cloud::NotFoundError
717
708
  nil
718
709
  end
719
- alias_method :get_sink, :sink
720
- alias_method :find_sink, :sink
710
+ alias get_sink sink
711
+ alias find_sink sink
721
712
 
722
713
  ##
723
714
  # Retrieves the list of metrics belonging to the project.
@@ -753,7 +744,7 @@ module Google
753
744
  grpc = service.list_metrics token: token, max: max
754
745
  Metric::List.from_grpc grpc, service, max
755
746
  end
756
- alias_method :find_metrics, :metrics
747
+ alias find_metrics metrics
757
748
 
758
749
  ##
759
750
  # Creates a new logs-based metric for Google Cloud Monitoring.
@@ -786,7 +777,7 @@ module Google
786
777
  grpc = service.create_metric name, filter, description
787
778
  Metric.from_grpc grpc, service
788
779
  end
789
- alias_method :new_metric, :create_metric
780
+ alias new_metric create_metric
790
781
 
791
782
  ##
792
783
  # Retrieves metric by name.
@@ -815,8 +806,8 @@ module Google
815
806
  rescue Google::Cloud::NotFoundError
816
807
  nil
817
808
  end
818
- alias_method :get_metric, :metric
819
- alias_method :find_metric, :metric
809
+ alias get_metric metric
810
+ alias find_metric metric
820
811
 
821
812
  protected
822
813
 
@@ -824,7 +815,7 @@ module Google
824
815
  # @private Raise an error unless an active connection to the service is
825
816
  # available.
826
817
  def ensure_service!
827
- fail "Must have active connection to service" unless service
818
+ raise "Must have active connection to service" unless service
828
819
  end
829
820
  end
830
821
  end