ons-json-logger 1.0.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: 759ed29d813c6ac95eb31020797758b86b81eb1ac22e5139cda8f3fa3b107794
4
+ data.tar.gz: 03c07936733e47cb2863d880a27855a0ede25adcf9a09a6895429590302544f1
5
+ SHA512:
6
+ metadata.gz: 7043230cc29dab6ecab1915536eb8874527196d8cddba9d93ad177f07814bcf3dc9b1502f1c01e17e58ea09d92520d214718df2c96d9f4657cb35926da591e61
7
+ data.tar.gz: 7099ba5e33de14fba486d998e387575c4de4848eb0da3229d71ef2a0bab9c0c036b58833510dc653778b5e8d6097fb522901055b91d20dc7876c66c8cfd56fdf
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # ONS JSON Logger RubyGem
2
+ A utility class for generating structured JSON log entries with optional additional fields suitable for web applications. Note that this gem targets Ruby 3.1 and above.
3
+
4
+ ## Installation
5
+ ```
6
+ gem install ons-json-logger
7
+ ```
8
+
9
+ ## Examples
10
+
11
+ ```ruby
12
+ require 'logger'
13
+ require 'ons-json-logger'
14
+ require 'sinatra'
15
+
16
+ LOGGER = Logger.new($stdout)
17
+ JSON_LOGGER = JSONLogger.new(application: 'amazing-webapp', environment: 'production')
18
+
19
+ get '/signout' do
20
+ LOGGER.info(JSON_LOGGER.log(level: 'INFO',
21
+ message: 'User signed out',
22
+ module_name: 'app',
23
+ user: { id: @user_login_id, ip_address: @client_ip },
24
+ http: { method: 'GET', path: '/signout', status: 200, user_agent: request.user_agent }))
25
+ end
26
+ ```
27
+
28
+ ## Testing
29
+ ```
30
+ rake test
31
+ ```
32
+
33
+ ## Licence
34
+ This library is licensed under the MIT licence. Full licence text is available in [LICENCE](LICENCE).
35
+
36
+ ## Copyright
37
+ Copyright (C) 2024 Crown Copyright (Office for National Statistics)
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'time'
5
+
6
+ # Class that generates structured JSON log entries.
7
+ class JSONLogger
8
+ # Constructor that initialises the JSON logger.
9
+ #
10
+ # @param application [String] the name of the application
11
+ # @param environment [String] the name of the environment the application is deployed to. Defaults to +development+
12
+ # @raise [ArgumentError] if application is nil
13
+ def initialize(application:, environment: 'development')
14
+ raise ArgumentError, 'application cannot be nil' if application.nil?
15
+
16
+ @application = application
17
+ @environment = environment
18
+ end
19
+
20
+ # Returns a structured JSON log entry from the passed arguments.
21
+ #
22
+ # @param level the log level e.g. +DEBUG+, +ERROR+, +INFO+
23
+ # @param message the log message
24
+ # @param module_name the name of the application module/component generating the log message
25
+ # user optional Hash containing details of the user making the request such as username and IP address
26
+ # http optional Hash containing details of the HTTP request such as the method, path, status code and user agent
27
+ # error optional Hash containing details of an error that occurred such as the error code and message
28
+ # @raise [ArgumentError] if level is nil
29
+ # @raise [ArgumentError] id message is nil
30
+ # @raise [ArgumentError] if module_name is nil
31
+ def log(level:, message:, module_name:, user: {}, http: {}, error: {})
32
+ raise ArgumentError, 'level cannot be nil' if level.nil?
33
+ raise ArgumentError, 'message' if level.nil?
34
+ raise ArgumentError, 'module_name cannot be nil' if level.nil?
35
+
36
+ log_entry = {
37
+ timestamp: Time.now.utc.iso8601,
38
+ level:,
39
+ message:,
40
+ application: @application,
41
+ environment: @environment,
42
+ module: module_name
43
+ }
44
+
45
+ # Add optional fields if they are provided.
46
+ log_entry[:user] = user unless user.empty?
47
+ log_entry[:http] = http unless http.empty?
48
+ log_entry[:error] = error unless error.empty?
49
+
50
+ JSON.generate(log_entry)
51
+ end
52
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ONSJSONLogger
4
+ module Version
5
+ MAJOR = 1
6
+ MINOR = 0
7
+ TINY = 0
8
+ end
9
+ VERSION = [Version::MAJOR, Version::MINOR, Version::TINY].compact * '.'
10
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ons-json-logger/json_logger'
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ons-json-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Topley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'A utility class for generating structured JSON log entries with optional
14
+ additional fields suitable for web applications.
15
+
16
+ '
17
+ email:
18
+ - john.topley@ons.gov.uk
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - README.md
24
+ - lib/ons-json-logger.rb
25
+ - lib/ons-json-logger/json_logger.rb
26
+ - lib/ons-json-logger/version.rb
27
+ homepage: https://github.com/ONSdigital/ons-json-logger
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ rubygems_mfa_required: 'true'
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.1.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.3.15
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: A class for generating structured JSON log entries.
51
+ test_files: []