Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Mar 20, 2024
0 parents commit 37f8f3e
Show file tree
Hide file tree
Showing 32 changed files with 886 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/rake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Auto-generated by Cimas: Do not edit it manually!
# See https://github.com/metanorma/cimas
name: rake

on:
push:
branches: [ master, main ]
tags: [ v* ]
pull_request:

jobs:
rake:
uses: relaton/support/.github/workflows/rake.yml@main
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Auto-generated by Cimas: Do not edit it manually!
# See https://github.com/metanorma/cimas
name: release

on:
workflow_dispatch:
inputs:
next_version:
description: |
Next release version. Possible values: x.y.z, major, minor, patch (or pre|rc|etc).
Also, you can pass 'skip' to skip 'git tag' and do 'gem push' for the current version
required: true
default: 'skip'
repository_dispatch:
types: [ do-release ]


jobs:
release:
uses: relaton/support/.github/workflows/release.yml@main
with:
next_version: ${{ github.event.inputs.next_version }}
secrets:
rubygems-api-key: ${{ secrets.RELATON_CI_RUBYGEMS_API_KEY }}
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
/.vscode/

# rspec failure tracking
.rspec_status
.rubocop-https---raw-githubusercontent-com-riboseinc-oss-guides-master-ci-rubocop-yml
Gemfile.lock
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
12 changes: 12 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This project follows the Ribose OSS style guide.
# https://github.com/riboseinc/oss-guides
# All project-specific additions and overrides should be specified in this file.

require: rubocop-rails

inherit_from:
- https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
AllCops:
TargetRubyVersion: 2.7
Rails:
Enabled: false
15 changes: 15 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in relaton-logger.gemspec
gemspec

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"

gem "rubocop", "~> 1.21"
gem "rubocop-rails", "~> 2.0"

gem "simplecov", "~> 0.21"
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Andrei Kislichenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
132 changes: 132 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
= Relaton::Logger

image:https://img.shields.io/gem/v/relaton-logger.svg["Gem Version", link="https://rubygems.org/gems/relaton-logger"]
image:https://github.com/relaton/relaton-logger/workflows/rake/badge.svg["Build Status", link="https://github.com/relaton/relaton-logger/actions?workflow=rake"]
image:https://codeclimate.com/github/relaton/relaton-logger/badges/gpa.svg["Code Climate", link="https://codeclimate.com/github/relaton/relaton-logger"]
image:https://img.shields.io/github/issues-pr-raw/relaton/relaton-logger.svg["Pull Requests", link="https://github.com/relaton/relaton-logger/pulls"]
image:https://img.shields.io/github/commits-since/relaton/relaton-logger/latest.svg["Commits since latest",link="https://github.com/relaton/relaton-logger/releases"]

Relaton::Logger is a Ruby gem that implements a logger for Relaton gems. It is also possible to use this gem as a standalone logger.

== Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add relaton-logger

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install relaton-logger

== Usage

The Relaton::Logger gem provides a logger pool that can be used to log messages. The logger pool is a singleton and can be accessed by calling `Relaton.logger_pool`. The logger pool is an Array that contains loggers. By default, the logger pool contains a single logger, which is an instance of `Relaton::Logger::Log` class, which inherits the `Logger` class from the Ruby standard library.

The logger pool can be used to log messages by calling the methods of the loggers in the pool. These methods are the same as the methods of the `Logger` class from the Ruby standard library (`debug`, `info`, `warn`, `error`, `fatal`, `unknown`).

[source, ruby]
----
require "relaton/logger"
Relaton.logger_pool.info "message"
INFO: message
----

The program name and the key can be passed as arguments to the log methods. The program name is a string that is added to the log message. The key is a string that is added to the log message in parentheses.

[source, ruby]
----
Relaton.logger_pool.warn "message", "progname", key: "KEY"
[progname] WARN: (KEY) message
----

The block form of the log methods can be used to log messages that are expensive to generate. The block is called only if the message is logged.

[source, ruby]
----
Relaton.logger_pool.error("progname", key: "KEY") { "message" }
[progname] ERROR: (KEY) message
----

The logger pool can configured to contain multiple loggers. The loggers in the pool are called in the order they are added to the pool. To add a logger to the pool, call the `<<` method of the logger pool and pass the logger as an argument.

[source, ruby]
----
Relaton::Logger.configure do |config|
config.logger_pool << Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn])
end
----

To replace the loggers in the pool, use `=` method of the logger pool and pass the new loggers as an argument.

[source, ruby]
----
Relaton::Logger.configure do |config|
config.logger_pool = [Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn])]
end
----

To create a new logger, call the `new` method of the `Relaton::Logger::Log` class and pass arguments:

- `logdev` - file name or an IO object. If nil or File::NULL, messages are not logged. If it is a file name, the file is opened in append mode. If it is an IO object, messages are written to it.
- `shift_age` - the number of old log files to keep. If 0, no old log files are kept. If it is a number, old log files are kept and the log file is rotated when it reaches the maximum size. If it is a string, old log files are kept and the log file is rotated when it reaches the maximum size. The string is a time unit and a number, for example, "daily 7" or "weekly 4". Default is 0.
- `shift_size` - the maximum size of the log file. If it is a number, the log file is rotated when it reaches the maximum size. If it is a string, the log file is rotated when it reaches the maximum size. The string is a number and a time unit, for example, "1048576" or "1M". Default is 1048576.
- `levels` - an array of symbols that represent the log levels The log levels are `:debug`, `:info`, `:warn`, `:error`, `:fatal`, `:unknown`. Default is `[:info, :warn, :error, :fatal, :unknown]`.
- `formatter` - a formatter object that is used to format the log messages. Two formatters are available at this moment `Relaton::Logger::FormatterString`, `Relaton::Logger::FormatterJson`, and `Proc`. The default formater is `Relaton::Logger::FormatterString`.
- `progname` - a string that is added to the log message. Default is nil.

[source, ruby]
----
Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn], formatter: Relaton::Logger::FormatterJSON, progname: "progname")
----

To create a custom formatter implement a class with the `call` method which takes agruments:

- `severity` - the log level
- `time` - the time the message was logged
- `progname` - the program name
- `msg` - the message
- any other keyword arguments that are passed to the log method, for example, `key`

[source, ruby]
----
class CustomFormatter
def call(severity, time, progname, msg, key: "Key 1")
"#{time} [#{severity}] #{progname} (#{key}): #{msg}\n"
end
end
Relaton::Logger::Log.new("relaton.log", formatter: CustomFormatter)
----

It's possible to use `Proc` as a formatter. The `Proc` object is called with the same arguments as the `call` method of the custom formatter.

[source, ruby]
----
log = Relaton::Logger::Log.new("relaton.log")
log.formatter = Proc.new do |severity, time, progname, msg, key: "Key 1"|
"#{time} [#{severity}] #{progname} (#{key}): #{msg}\n"
end
----

A log file can be cleared by calling the `truncate` method of the logger pool.

[source, ruby]
----
Relaton.logger_pool.truncate
----

== Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

== Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/relaton/relaton-logger.

== License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[spec]
11 changes: 11 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "relaton/logger"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
17 changes: 17 additions & 0 deletions lib/relaton/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "forwardable"
require "logger"
require_relative "logger/version"
require_relative "logger/log"
require_relative "logger/log_device"
require_relative "logger/pool"
require_relative "logger/formatter_string"
require_relative "logger/formatter_json"
require_relative "logger/config"

module Relaton
def self.logger_pool
Logger.configuration.logger_pool
end
end
34 changes: 34 additions & 0 deletions lib/relaton/logger/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Relaton::Logger
module Config
def configure
yield configuration if block_given?
end

def configuration
@configuration ||= self::Configuration.new
end
end

class Configuration
# @return [Array<Relaton::Logger::Log>] List of loggers
attr_reader :logger_pool

def initialize
@logger_pool ||= Pool.new
@logger_pool << Log.new($stderr, levels: %i[info warn error fatal])
end

#
# Replace the current list of loggers with the given list of loggers.
#
# @param [Array<Relaton::Logger::Log>] loggers list of loggers
#
# @return [void]
#
def logger_pool=(loggers)
@logger_pool.loggers = loggers
end
end

extend Config
end
8 changes: 8 additions & 0 deletions lib/relaton/logger/formatter_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Relaton::Logger
class FormatterJSON < ::Logger::Formatter
def call(severity, _datetime, progname, msg, **args)
hash = { prog: progname, message: msg, severity: severity }.merge(args)
"#{hash.to_json}\n"
end
end
end
12 changes: 12 additions & 0 deletions lib/relaton/logger/formatter_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Relaton::Logger
class FormatterString < ::Logger::Formatter
def call(severity, _datetime, progname, msg, **args)
output = []
output << "[#{progname}]" if progname
output << "#{severity}:"
output << "(#{args[:key]})" if args[:key]
output << "#{msg}\n"
output.join " "
end
end
end
Loading

0 comments on commit 37f8f3e

Please sign in to comment.