Skip to content

Commit

Permalink
MLogger for public release
Browse files Browse the repository at this point in the history
  • Loading branch information
aeroastro committed Jul 31, 2023
0 parents commit ab1da12
Show file tree
Hide file tree
Showing 20 changed files with 637 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test

on:
push:
branches:
- master

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '2.6'
- '2.7'
- '3.0'
- '3.1'
- '3.2'

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status

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
29 changes: 29 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
AllCops:
TargetRubyVersion: 2.6

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120

Layout/FirstHashElementIndentation:
EnforcedStyle: consistent

Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation

Layout/MultilineMethodCallBraceLayout:
EnforcedStyle: new_line

Style/Documentation:
Enabled: false

Metrics/BlockLength:
Exclude:
- spec/**/*.rb
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## [Unreleased]

## [1.0.0] - 2023-07-31

- Public Release

## [0.1.0] - 2023-07-03

- Initial release
24 changes: 24 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# Contributor Code of Conduct

As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information, such as physical or electronic addresses, without explicit permission
* Other unethical or unprofessional conduct

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.

This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.

This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 1.2.0, available at https://www.contributor-covenant.org/version/1/2/0/code-of-conduct.html

18 changes: 18 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in m_logger.gemspec
gemspec

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"

gem "rubocop", "~> 1.21"

gem "timecop"

gem "pry-byebug"

gem "parallel"
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) 2023 DeNA

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.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# MLogger

MLogger is a simple logger featuring an alternative log rotation strategy, specially designed to handle high traffic loads and transient environments.

To use MLogger, initiate a new logger with `MLogger.new('log/production.log', shift_period_suffix: '%Y%m%d_%H')`.

## Log Rotation Strategy

Here's how ordinary log rotation strategy works:

1. Logs are consistently written to the same file (e.g., `production.log`).
2. The file is renamed once a specified time or size limit is reached (e.g., `production.log` becomes `production.log.20230701`).

In contrast, MLogger employs a different log rotation strategy:

1. Each log message is written to a time-stamped file (e.g., `production.log.20230701_11`).
2. Upon reaching a time threshold, logging is switched to a new file with the updated timestamp (e.g., `production.log.20230701_12`).

Key advantages of using MLogger include:

1. Support for hourly and shorter shift periods, accommodating heavy traffic loads.
2. Log lines always exist within the appropriately time-stamped path, even in cases where there are no logs or when there is an overflow of logs beyond shift periods.

## Additional Features

1. For systems expecting an IO-like interface, you can utilize `MLogger::LoggerDevice` (`MLogger::LoggerDevice#write`).
2. To facilitate line-based operations, MLogger skips writing a log header (e.g., `# Logfile created on %s by %s`).

## Installation

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

$ bundle add m_logger

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

$ gem install m_logger

## Usage

```ruby
# Log to File
hourly_logger = MLogger.new('log/my_log', shift_period_suffix: '%Y%m%d_%H') # hourly rotate
hourly_logger.debug('fuga')

daily_logger = MLogger.new('log/my_log', shift_period_suffix: '%Y%m%d') # daily rotate
daily_logger.debug('fuga')

# Log in Rack::Middleware
logger_device = MLogger::Device.new('log/rack_ltsv', shift_period_suffix: '%Y%m%d_%H')
Rails.configuration.middleware.insert(0, Rack::LtsvLogger, logger_device)

# Log to Stdout (You don't have to change logger class.)
logger = MLogger.new(STDOUT)
logger.info('hoge')
```

## 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/DeNA/m_logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/DeNA/m_logger/blob/master/CODE_OF_CONDUCT.md).

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the MLogger project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/DeNA/m_logger/blob/master/CODE_OF_CONDUCT.md).

## Special Thanks

External log file specification originates from Mobage and Sakasho series in DeNA.
Thanks for those who have contributed to these products.
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 rubocop]
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 "m_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
41 changes: 41 additions & 0 deletions lib/m_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require "logger"

require_relative "m_logger/version"
require_relative "m_logger/log_device"

class MLogger < ::Logger
# rubocop:disable Lint/MissingSuper,Metrics/MethodLength,Metrics/ParameterLists
def initialize(logdev, shift_age = 0, shift_size = 1_048_576, level: DEBUG,
progname: nil, formatter: nil, datetime_format: nil,
binmode: false, shift_period_suffix: "%Y%m%d")

self.level = level
self.progname = progname
@default_formatter = Formatter.new
self.datetime_format = datetime_format
self.formatter = formatter
@logdev = nil
@level_override = {}

return unless logdev && logdev != File::NULL

# Only this part is different from original ::Logger
@logdev = if RUBY_VERSION >= "2.7.0"
MLogger::LogDevice.new(logdev,
shift_age: shift_age,
shift_size: shift_size,
shift_period_suffix: shift_period_suffix,
binmode: binmode
)
else
MLogger::LogDevice.new(logdev,
shift_age: shift_age,
shift_size: shift_size,
shift_period_suffix: shift_period_suffix
)
end
end
# rubocop:enable Lint/MissingSuper,Metrics/MethodLength,Metrics/ParameterLists
end
35 changes: 35 additions & 0 deletions lib/m_logger/log_device.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "logger"

class MLogger
class LogDevice < ::Logger::LogDevice
def initialize(log = nil, *args, shift_period_suffix: nil, **kwargs)
# When the output is file, save original name, and append shift_period_suffix.
is_file = log && !(log.respond_to?(:write) && log.respond_to?(:close))
if is_file
@original_filename = log
log = m_logger_filename(shift_period_suffix)
end

super(log, *args, shift_period_suffix: shift_period_suffix, **kwargs)
end

private

def check_shift_log
# Switch to a new file, when the time has come.
return unless @filename && @filename < (f = m_logger_filename(@shift_period_suffix))

reopen(f)
end

def add_log_header(_file)
# skip writing log header
end

def m_logger_filename(suffix)
"#{@original_filename}.#{Time.now.strftime(suffix)}"
end
end
end
7 changes: 7 additions & 0 deletions lib/m_logger/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require "logger"

class MLogger < ::Logger
VERSION = "0.1.0"
end
38 changes: 38 additions & 0 deletions m_logger.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require_relative "lib/m_logger/version"

Gem::Specification.new do |spec|
spec.name = "m_logger"
spec.version = MLogger::VERSION
spec.authors = ["Takumasa Ochi"]

spec.summary = "Simple Logger with Alternative Log Rotation Strategy"
spec.description = "Simple Logger with Alternative Log Rotation Strategy"
spec.homepage = "https://github.com/DeNA/m_logger"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"

# spec.metadata["allowed_push_host"] = ""

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/DeNA/m_logger"
spec.metadata["changelog_uri"] = "https://github.com/DeNA/m_logger/blob/master/CHANGELOG.md"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end
Loading

0 comments on commit ab1da12

Please sign in to comment.