oxy_logger 0.3.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: 49ec790e5c7675b55bcfacdea108a881a962aba4816b0fbd057198d810394650
4
+ data.tar.gz: 0c6ae15b91aeeeb2bf5b8d1cf4bc6e0789a764e2b31075a35b9e890f3495a0af
5
+ SHA512:
6
+ metadata.gz: 4e7ba48953768e0985a1e7985c4730f2071407ebc1ea19962a6ca085d0a80d574ffe7bc69fac72b555152a5714029828416e40429611d0516b2947191a2c8ead
7
+ data.tar.gz: 1545beb94aead03d7ffab827b4b38949637e273c4b65812d4d60fb789b3c25867430e9e0ca3f711a529a86fa8bf5e28fa8c82e201db1c6903f094af27f8c64b9
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /oxy_logger-*
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in oxy_logger.gemspec
6
+ gemspec
7
+ gem 'mustache', '~> 1.0'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Paqwtv
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # OxyLogger
2
+
3
+ Welcome to our Rails logger gem!
4
+
5
+ This is a simple logger that is easy to use and can be configured as you want.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'oxy_logger', '~> 0.3.0'
13
+ ```
14
+
15
+ Then run:
16
+
17
+ $ bundle install
18
+
19
+ ## Usage
20
+
21
+ To include gem tasks inside your rails app.
22
+ Add this code to rails `Rakefile`:
23
+
24
+ ```ruby
25
+ spec = Gem::Specification.find_by_name 'oxy_logger'
26
+ load "#{spec.gem_dir}/lib/tasks/logger_install.rake"
27
+ ```
28
+
29
+ Then run the rake task.
30
+ This command will create a configuration file in your application and run the `migration` to update your database.
31
+
32
+ $ rake oxy:install
33
+
34
+ ## Controller or Model logging
35
+
36
+ In order to log the controller methods, add in `app/controllers/application_controller.rb`
37
+ Do the same code for the Model, if you need to log it `app/models/application_record.rb`
38
+
39
+ ```ruby
40
+ extend OxyLogger::Sys
41
+ include OxyLogger::Helper
42
+ ```
43
+
44
+ And at the end, specify what methods you want to use for logging.
45
+ IMPORTANT! The method `log_it` must be at the end of the logged methods.
46
+
47
+ ```ruby
48
+ logg_it [ :index, :show, :new, :edit, :create, :update, :destroy ] # Select any of the available methods
49
+ ```
50
+
51
+ Very simple isn't?
52
+
53
+ ## Configuration
54
+
55
+ When you launched the rake task a configuration file was copied into your project, which you can find at `/config/initializers/logger_init.rb`
56
+
57
+ ```ruby
58
+ OxyLogger.configure do |config|
59
+ # Save logs locally to a file or database
60
+ config.save_to = "file" # or config.save_to = "db"
61
+ ```
62
+
63
+ If you want to log to a file, you can specify the path to it.
64
+ If this file does not exist, the **OxyLogger** will create it.
65
+
66
+ ```ruby
67
+ # config.files_path = Rails.root + "/log/logged_data" # the same things in example
68
+ config.files_path = Rails.root.join('log', 'logged_data')
69
+ ```
70
+
71
+ You can choose which parameters you want to record.
72
+ Unnecessary parameter just comment out or delete.
73
+
74
+ ```ruby
75
+ config.logget_fields = %i[
76
+ date_time # called time
77
+ class_name # class in which was called
78
+ method_name # method in which was called
79
+ result # return result
80
+ params # parameters
81
+ ]
82
+ ```
83
+
84
+ ## Contributing
85
+
86
+ We will be very happy if our product interests you and you will have an irresistible desire to help us with its development.
87
+
88
+ https://github.com/[USERNAME]/oxy_logger.
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'oxy_logger'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,52 @@
1
+ require 'rails'
2
+ require 'oxy_logger'
3
+
4
+ class Object
5
+ def self.method_hook(*args)
6
+ options = args.extract_options!
7
+ return unless (options[:before].present? or options[:after].present?)
8
+ args.each do |method_name|
9
+ old_method = instance_method(method_name) rescue next
10
+
11
+ define_method(method_name) do |*args|
12
+ # invoke before callback
13
+ if options[:before].present?
14
+ options[:before].is_a?(Proc) ? options[:before].call(method_name, self):
15
+ send(options[:before], method_name, *args)
16
+ end
17
+
18
+ # you can modify the code to call after callback
19
+ # only when the old method returns true etc..
20
+ old_method.bind(self).call(*args)
21
+
22
+ # invoke after callback
23
+ if options[:after].present?
24
+ options[:after].is_a?(Proc) ? options[:after].call(method_name, self):
25
+ send(options[:after], method_name, *args)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ # Default configure Logger GEM
33
+ OxyLogger.configure do |config|
34
+ # @note - Save logs locally to a file or database
35
+ # @param value [String] - "file" or "db"
36
+ # @example
37
+ # config.save_to = "db"
38
+ config.save_to = 'file'
39
+ # @param value [String] - way to save logs
40
+ # @example
41
+ # config.files_path = Rails.root + "/log/logged_data" #the same things in example
42
+ config.files_path = Rails.root.join('log', 'logged_data')
43
+ # @note - logged parameters
44
+ config.logget_fields = %i[
45
+ date_time
46
+ class_name
47
+ method_name
48
+ result
49
+ params
50
+ ]
51
+ # TODO add 'run_time' parametrs
52
+ end
data/lib/oxy_logger.rb ADDED
@@ -0,0 +1,79 @@
1
+ require "oxy_logger/version"
2
+ require "oxy_logger/writer"
3
+
4
+ module OxyLogger
5
+ module Sys
6
+ def logg_it(my_names)
7
+ my_names.each do |my_name|
8
+ method_hook( my_name, before: :log_befor )
9
+ method_hook( my_name, after: :log_after )
10
+ end
11
+ end
12
+ end
13
+
14
+ module Helper
15
+ def log_befor(method_name, *args)
16
+ first_data = {}
17
+ type = self.class.superclass.to_s
18
+ first_data[:start] = DateTime.now
19
+ first_data[:name] = method_name
20
+ first_data[:class_name] = self.class.name
21
+ if type == "ApplicationRecord"
22
+ first_data[:type] = :model
23
+ else
24
+ first_data[:type] = :controller
25
+ end
26
+ first_data[:params] = first_data[:type] == :model ?
27
+ first_data[:args] = args :
28
+ first_data[:args] = params
29
+ Writer.write(first_data)
30
+ end
31
+
32
+ def log_after(method_name, *args); end
33
+ end
34
+
35
+ @@files_path
36
+ # @param value [String] - путь куда сохранять логи
37
+ def self.files_path=(value)
38
+ @@files_path = value
39
+ end
40
+
41
+ # @param value [String] - "file" or "db"
42
+ @@save_to
43
+ def self.save_to=(value)
44
+ @@save_to = value
45
+ end
46
+
47
+ @@logget_fields
48
+ def self.logget_fields=(value)
49
+ @@logget_fields = value
50
+ end
51
+
52
+ def self.logget_data
53
+ @@logget_fields
54
+ end
55
+
56
+ def self.path_to_log
57
+ @@files_path
58
+ end
59
+
60
+ # Конфигурирует сам себя
61
+ def self.configure
62
+ yield self
63
+ end
64
+
65
+ # @note метод, возвращающий настройки всего приложения после инициализации
66
+ # @example
67
+ # OxyLogger.config_oxy_hash
68
+ # @return [Hash]
69
+ # OxyLogger.config_oxy_hash => {:files_path=>"~/projects/logger", :save_to_file_or_db=>"file",
70
+ # :incoming_params=>true, :output_params=>true, :processing_time=>true,
71
+ # :date_time=>true, :called_method=>true, :class_name=>true}
72
+ def self.config_oxy_hash
73
+ {
74
+ files_path: @@files_path,
75
+ save_to: @@save_to,
76
+ logget_fields: @@logget_fields
77
+ }
78
+ end
79
+ end
@@ -0,0 +1,16 @@
1
+ module OxyLogger
2
+ module Formatter
3
+ def self.format_data(first_data)
4
+ data = {}
5
+ data[:run_time] = first_data[:work_time]
6
+ data[:date_time] = first_data[:start]
7
+ data[:class_name] = first_data[:class_name]
8
+ data[:method_name] = first_data[:name]
9
+ data[:result] = first_data[:result]
10
+ data[:params] = first_data[:params]
11
+ data[:type] = first_data[:type]
12
+ data[:save_to] = OxyLogger.config_oxy_hash[:save_to].to_sym
13
+ data
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ require 'mustache'
2
+
3
+ module OxyLogger
4
+ class LogRecord
5
+ def initialize(data)
6
+ @data = data
7
+ end
8
+
9
+ def save_to
10
+ @data[:save_to]
11
+ end
12
+
13
+ def for_db
14
+ @data.except(:save_to, :type)
15
+ end
16
+
17
+ def file_name
18
+ @data[:class_name] + '.log'
19
+ end
20
+
21
+ def for_file
22
+ text = @data.slice(OxyLogger.logget_data)
23
+ Mustache.template_file = "#{__dir__}/template.mustache"
24
+ str = Mustache.render(@data)
25
+ str
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ LogRecord for:-------{{type}}
2
+ {{# class_name}}
3
+ Logging at class:----{{class_name}}
4
+ {{/ class_name}}
5
+ {{# called_method}}
6
+ In method:-----------{{method_name}}
7
+ {{/ called_method}}
8
+ {{# date_time}}
9
+ Logging time:--------{{date_time}}
10
+ {{/ date_time}}
11
+ {{# processing_time}}
12
+ Processing time is:--{{run_time}}
13
+ {{/ processing_time}}
14
+ {{# output_params}}
15
+ Result:--------------{{result}}
16
+ {{/ output_params}}
17
+ {{# incoming_params}}
18
+ Incomming params:----{{params}}
19
+ {{/ incoming_params}}
20
+ _________________________________________________
@@ -0,0 +1,3 @@
1
+ module OxyLogger
2
+ VERSION = '0.3.0'
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'rails'
2
+ require 'active_record'
3
+ require_relative 'formatter'
4
+ require_relative 'log_record'
5
+
6
+ module OxyLogger
7
+ module Writer
8
+ environment = ENV['RACK_ENV'] || 'development'
9
+ dbconfig = YAML.load(File.read('config/database.yml'))
10
+ ActiveRecord::Base.establish_connection(dbconfig[environment])
11
+
12
+ class LogDb < ActiveRecord::Base
13
+ end
14
+
15
+ def self.write(first_data)
16
+ data = Formatter.format_data(first_data)
17
+ record = LogRecord.new(data)
18
+ record.save_to == :db ?
19
+ save_to_db(record.for_db) :
20
+ save_to_file(record.file_name, record.for_file)
21
+ end
22
+
23
+ def self.check_path
24
+ if OxyLogger.path_to_log.exist? == false
25
+ Dir.mkdir(OxyLogger.path_to_log)
26
+ end
27
+ end
28
+
29
+ def self.save_to_file(file_name, text)
30
+ Writer.check_path
31
+ path = [OxyLogger.path_to_log, file_name].join('/')
32
+
33
+ File.open(path, "a") do |f|
34
+ f.print("#{text}\n")
35
+ end
36
+ end
37
+
38
+ def self.save_to_db(data)
39
+ LogDb.create do |log|
40
+ log.run_time = data[:run_time]
41
+ log.class_name = data[:class_name]
42
+ log.date_time = data[:date_time]
43
+ log.method_name = data[:method_name]
44
+ log.result = data[:result]
45
+ log.params = data[:params]
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,27 @@
1
+
2
+ # To include your task inside rails app you need add this to rails Rakefile:
3
+
4
+ # spec = Gem::Specification.find_by_name 'oxy_logger'
5
+ # load '#{spec.gem_dir}/lib/tasks/logger_install.rake'
6
+ # ================================================
7
+ # Type 'rake oxy:install' in terminal to run the task
8
+ # ================================================
9
+
10
+ require 'rake'
11
+ namespace :oxy do
12
+ desc 'Task for copy config file from gem to rails'
13
+ task :logger_init do
14
+ cp(File.join(Gem.loaded_specs['oxy_logger'].full_gem_path,'config', 'logger_init.rb'), File.join(Rails.root,'config','initializers','logger_init.rb'))
15
+ end
16
+ desc 'Task for create oxy_logger table file in your rails app'
17
+ task :logger_table do
18
+ system 'rails g migration CreateLogger user:string run_time:datetime date_time:datetime class_name:string method_name:string result:string params:string'
19
+ end
20
+ desc 'Task for accept migration to db'
21
+ task :migrate do
22
+ system 'rake db:migrate'
23
+ end
24
+ desc 'install is a :logger_init and :logger_table'
25
+ task install: [:logger_init, :logger_table, :migrate] do
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "oxy_logger/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "oxy_logger"
8
+ spec.version = OxyLogger::VERSION
9
+ spec.authors = ["Dima, Vitalik, Vlad"]
10
+ spec.email = ["paqwtv@gmail.com"]
11
+ spec.summary = %q{Ruby logger gem}
12
+ spec.description = %q{This is a simple logger that is easy to use and can be configured as you want.}
13
+ spec.homepage = "https://github.com/Paqwtv/oxy_logger"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org/'
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against "
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.15"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "yard", "~> 0.9.9"
36
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oxy_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Dima, Vitalik, Vlad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.9
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.9
69
+ description: This is a simple logger that is easy to use and can be configured as
70
+ you want.
71
+ email:
72
+ - paqwtv@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - config/logger_init.rb
87
+ - lib/oxy_logger.rb
88
+ - lib/oxy_logger/formatter.rb
89
+ - lib/oxy_logger/log_record.rb
90
+ - lib/oxy_logger/template.mustache
91
+ - lib/oxy_logger/version.rb
92
+ - lib/oxy_logger/writer.rb
93
+ - lib/tasks/logger_install.rake
94
+ - oxy_logger.gemspec
95
+ homepage: https://github.com/Paqwtv/oxy_logger
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ allowed_push_host: https://rubygems.org/
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.7.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Ruby logger gem
120
+ test_files: []