Skip to content

Commit

Permalink
Receive logger as param (#3)
Browse files Browse the repository at this point in the history
* Receive logger as param

* Fix tagger

* Rename

* Remove Pry
  • Loading branch information
banduk authored May 24, 2023
1 parent 37c881e commit d151506
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 187 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ gem "rake", "~> 12.0"
gem "rspec", "~> 3.0"

gem 'byebug'
gem 'pry'

gem 'simplecov', require: false
gem 'simplecov-lcov', require: false
8 changes: 1 addition & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
PATH
remote: .
specs:
pier_console_detective (0.2.0)
pier_console_detective (0.3.0)

GEM
remote: https://rubygems.org/
specs:
byebug (11.1.3)
coderay (1.1.3)
diff-lcs (1.4.4)
docile (1.4.0)
method_source (1.0.0)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
rake (12.3.3)
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand Down Expand Up @@ -42,7 +37,6 @@ PLATFORMS
DEPENDENCIES
byebug
pier_console_detective!
pry
rake (~> 12.0)
rspec (~> 3.0)
simplecov
Expand Down
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# ConsoleDetective ![Console Detective](https://github.com/arunn/pier_console_detective/actions/workflows/ci.yml/badge.svg)[![Codacy Badge](https://app.codacy.com/project/badge/Grade/7fabacab5ff445248655e1b9b35f1aef)](https://www.codacy.com?utm_source=github.com&utm_medium=referral&utm_content=arunn/pier_console_detective&utm_campaign=Badge_Grade)[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/7fabacab5ff445248655e1b9b35f1aef)](https://www.codacy.com?utm_source=github.com&utm_medium=referral&utm_content=arunn/pier_console_detective&utm_campaign=Badge_Coverage)[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/)[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/arunn/pier_console_detective/graphs/commit-activity)
# PierConsoleDetective ![Console Detective](https://github.com/arunn/pier_console_detective/actions/workflows/ci.yml/badge.svg)[![Codacy Badge](https://app.codacy.com/project/badge/Grade/7fabacab5ff445248655e1b9b35f1aef)](https://www.codacy.com?utm_source=github.com&utm_medium=referral&utm_content=arunn/pier_console_detective&utm_campaign=Badge_Grade)[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/7fabacab5ff445248655e1b9b35f1aef)](https://www.codacy.com?utm_source=github.com&utm_medium=referral&utm_content=arunn/pier_console_detective&utm_campaign=Badge_Coverage)[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/)[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/arunn/pier_console_detective/graphs/commit-activity)

A gem to track commands typed in rails console along with tagging in realtime. The tags can be used to identify users. This works with plain [pry](https://github.com/pry/pry) and [IRB](https://github.com/ruby/ruby/tree/master/lib/irb) and rails console using pry or IRB. The values for log tags, log format, log file name, and memoization requirements are configurable.
A gem to track commands typed in rails console along with tagging in realtime. The tags can be used to identify users. This works with [IRB](https://github.com/ruby/ruby/tree/master/lib/irb) and rails console using IRB. The values for tag, logger, and memoization requirements are configurable.

Pry and IRB both provide options for recording history. It has a few disadvantages:
IRB provide options for recording history. It has a few disadvantages:

1. It is not possible to get the logs in realtime.
2. There are no tagging options available.
Expand Down Expand Up @@ -30,7 +30,7 @@ $ gem install pier_console_detective
~~~~~
## Usage

There are meaningful defaults for the config. If you are using rails, run `rails console`. Otherwise, `irb -rpier_console_detective` or `pry -I./lib -r pier_console_detective` will load the respective consoles with `pier_console_detective` loaded. You can also modify `~/.irbrc` or `~/.pryrc` with `require pier_console_detective` to load the `pier_console_detective` by default.
There are meaningful defaults for the config. If you are using rails, run `rails console`. Otherwise, `irb -rpier_console_detective` will load the respective consoles with `pier_console_detective` loaded. You can also modify `~/.irbrc` with `require pier_console_detective` to load the `pier_console_detective` by default.

![demo_pier_console_detective](https://gist.githubusercontent.com/arunn/0a2795f1699c9e3c518ce20d7f5c1b16/raw/b20cb7d3aab4924311e59050a35237abd6a9a670/demo_pier_console_detective.gif)

Expand All @@ -39,30 +39,26 @@ The configs can be overridden by creating a file named `pier_console_detective.r
~~~ruby
require 'pier_console_detective'

ConsoleDetective.setup do |config|
# log_file_name is a string/file that mentions the location and name of the file where log will be written.
# default value is log/console.log
config.log_file_name = "log/console.log"
PierConsoleDetective.setup do |config|
# logger is a logger
# default value is Logger.new(STDOUT)
config.logger = Rails.logger

# log_tags is a lambda outputting the tag to tag the log entry
# log_tag is a lambda outputting the tag to tag the log entry
# default value is the ENV['USER']
config.log_tags = -> { ENV['USER'] }
config.log_tag = -> { ENV['USER'] }

# log_format is a lambda which takes tag and command as input and outputs the format in which the log will be entered in the log file
# default format is a hash of format { :tag => <tag>, :command => <command> }
config.log_format = -> (tag, command) { { tag: tag, command: command } }

# tag_memoization is a boolean to mention if the tag should be memoized or not.
# default is true
config.tag_memoization = true
end
~~~

If you are using rails, place this file in `config/initializers` folder and run `rails console`. Otherwise, `irb -r ./pier_console_detective.rb` or `pry -r ./pier_console_detective.rb` will load the respective consoles with `pier_console_detective` loaded with modified config. You can also modify `~/.irbrc` or `~/.pryrc` with `require pier_console_detective.rb` to load the modified config by default.
If you are using rails, place this file in `config/initializers` folder and run `rails console`. Otherwise, `irb -r ./pier_console_detective.rb` will load the respective consoles with `pier_console_detective` loaded with modified config. You can also modify `~/.irbrc` with `require pier_console_detective.rb` to load the modified config by default.

## 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. `bin/console` will run in IRB whereas `PRY=true bin/console` will run with Pry.
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. `bin/console` will run in IRB.

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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

Expand Down
14 changes: 3 additions & 11 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@ require 'byebug'
# 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.

# (If you use this, don't forget to add pry to your Gemfile!)

if(ENV['PRY'] == 'true')
require "pry"
require "pier_console_detective" # Loading after loading irb/pry since we want those to be present when loading pier_console_detective
require "irb"
require "pier_console_detective" # Loading after loading irb since we want those to be present when loading pier_console_detective

Pry.start
else
require "irb"
require "pier_console_detective" # Loading after loading irb/pry since we want those to be present when loading pier_console_detective

IRB.start(__FILE__)
end
IRB.start(__FILE__)
2 changes: 1 addition & 1 deletion bin/get_current_version.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
print Gem::Specification::load('pier_logging.gemspec').version.to_s
print Gem::Specification::load('pier_console_detective.gemspec').version.to_s
20 changes: 8 additions & 12 deletions lib/pier_console_detective.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
require "pier_console_detective/version"
require "pier_console_detective/mod_attr_accessor"
require "logger"

module ConsoleDetective
extend ConsoleDetective::ModAttrAccessor
module PierConsoleDetective
extend PierConsoleDetective::ModAttrAccessor

def self.setup
yield self
end

# log_file_name is a string/file that mentions the location and name of the file where log will be written.
# default value is log/console.log
mod_attr_accessor :log_file_name, "log/console.log"
# logger is a logger
# default value is Logger.new(STDOUT)
mod_attr_accessor :logger, Logger.new(STDOUT)

# log_tags is a lambda outputting the tag to tag the log entry
# log_tag is a lambda outputting the tag to tag the log entry
# default value is the ENV['USER']
mod_attr_accessor :log_tags, -> { ENV['USER'] }
mod_attr_accessor :log_tag, -> { ENV['USER'] }

# log_format is a lambda which takes tag and command as input and outputs the format in which the log will be entered in the log file
# default format is a hash of format { :tag => <tag>, :command => <command> }
mod_attr_accessor :log_format, -> (tag, command) { { tag: tag, command: command } }

# tag_memoization is a boolean to mention if the tag should be memoized or not.
# default is true
mod_attr_accessor :tag_memoization, true
end

require 'pier_console_detective/utils'
require 'pier_console_detective/irb'
require 'pier_console_detective/pry'
11 changes: 7 additions & 4 deletions lib/pier_console_detective/irb.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module ConsoleDetective
module PierConsoleDetective
module IrbLogger
def evaluate(*args, **kw)
ConsoleDetective::Utils.log_command(args.first.chomp)
super(*args, **kw)
input = args.first.chomp
PierConsoleDetective::Utils.log_command(input)
output = super(*args, **kw)
PierConsoleDetective::Utils.log_command_output(input, output)
output
end
end
end

IRB::Context.prepend(ConsoleDetective::IrbLogger) if defined?(IRB::Context)
IRB::Context.prepend(PierConsoleDetective::IrbLogger) if defined?(IRB::Context)
2 changes: 1 addition & 1 deletion lib/pier_console_detective/mod_attr_accessor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Inspired and simplified from ActiveSupport's defintion of mattr_accessor
# https://github.com/rails/rails/blob/5db5de534106a44070374810a99853f38843b1d2/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb

module ConsoleDetective
module PierConsoleDetective
module ModAttrAccessor
def mod_attr_accessor(attr_name, default_value)
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
Expand Down
9 changes: 0 additions & 9 deletions lib/pier_console_detective/pry.rb

This file was deleted.

38 changes: 31 additions & 7 deletions lib/pier_console_detective/utils.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
require 'logger'

module ConsoleDetective
module PierConsoleDetective
module Utils
LOGGER_PROC = ->(command) { ConsoleDetective::Utils.logger.info(ConsoleDetective.log_format.call(ConsoleDetective::Utils.get_tag, command)) }
LOGGER_PROC = ->(message, data) do
PierConsoleDetective::Utils.logger.info(message, {
data: data,
tag: PierConsoleDetective::Utils.get_tag,
})
end

def self.logger
@logger ||= Logger.new(ConsoleDetective.log_file_name)
@logger ||= PierConsoleDetective.logger
end

def self.get_tag
return @tag if ConsoleDetective.tag_memoization && @tag
@tag = ConsoleDetective.log_tags.call
return @tag if PierConsoleDetective.tag_memoization && @tag
@tag = PierConsoleDetective.log_tag.call
end

def self.log_command(command, immediately: false)
return Thread.new { ConsoleDetective::Utils::LOGGER_PROC.call(command) } unless immediately
ConsoleDetective::Utils::LOGGER_PROC.call(command)
return Thread.new {
PierConsoleDetective::Utils::LOGGER_PROC.call("Command executed", {
command: command.inspect
})
} unless immediately
PierConsoleDetective::Utils::LOGGER_PROC.call("Command executed", {
command: command.inspect
})
end

def self.log_command_output(command, output, immediately: false)
return Thread.new {
PierConsoleDetective::Utils::LOGGER_PROC.call("Command outputed", {
command: command.inspect,
output: output.inspect,
})
} unless immediately
PierConsoleDetective::Utils::LOGGER_PROC.call("Command outputed", {
command: command.inspect,
output: output.inspect,
})
end
end
end
4 changes: 2 additions & 2 deletions lib/pier_console_detective/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ConsoleDetective
VERSION = "0.2.0".freeze
module PierConsoleDetective
VERSION = "0.3.0".freeze
end
2 changes: 1 addition & 1 deletion pier_console_detective.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require_relative 'lib/pier_console_detective/version'

Gem::Specification.new do |spec|
spec.name = "pier_console_detective"
spec.version = ConsoleDetective::VERSION
spec.version = PierConsoleDetective::VERSION
spec.authors = ["ArunkumarN"]
spec.email = ["[email protected]"]
spec.licenses = ["MIT"]
Expand Down
43 changes: 0 additions & 43 deletions spec/dummy_console/pry.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/pier_console_detective/irb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "spec_helper"

RSpec.describe "ConsoleDetective::Irb" do
RSpec.describe "PierConsoleDetective::Irb" do
it "calls log command when evaluate is called" do
random_string = "random_string_irb"
expect(ConsoleDetective::Utils).to receive(:log_command).with("puts '#{random_string}';exit")
expect(PierConsoleDetective::Utils).to receive(:log_command).with("puts '#{random_string}';exit")
File.open('tmp/commands.rb', 'w') {|file| file.truncate(0) }
File.open("tmp/commands.rb", "w+") {|f| f.write("puts '#{random_string}';exit") }
IRB.conf[:SCRIPT] = "tmp/commands.rb"
Expand Down
23 changes: 0 additions & 23 deletions spec/pier_console_detective/pry_spec.rb

This file was deleted.

Loading

0 comments on commit d151506

Please sign in to comment.