log_in_file 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +24 -0
- data/README.md +83 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/log_in_file/logif_module.rb +36 -0
- data/lib/log_in_file/main.rb +94 -0
- data/lib/log_in_file/version.rb +3 -0
- data/lib/log_in_file.rb +8 -0
- data/log_in_file.gemspec +31 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: de8f050430a0366393ca7534b358ec9e7b8a613ecd747c34715a8c42fda22f17
|
4
|
+
data.tar.gz: 10b00ec0d6f3e4015287e41fc17e075820574195d92ef26429194b1cf3eb1a1a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 38090265cc83ab05006df63d73ad40bdf852aca1e17b8950f2c317d266e93147692c016289ef38759ea59f95ed14bcae1d6caec4b69e1f831e3e2de51762c01d
|
7
|
+
data.tar.gz: 76cd3f8ac04e24e3e2d49adb10ce33f88bc52f93e60573ffa360e7ff61531096eff61c126aff83d7121830bc2d554879b918c918d60889667fde49bead08f362
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
log_in_file (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.20.0)
|
10
|
+
minitest-color (0.0.2)
|
11
|
+
minitest (~> 5)
|
12
|
+
rake (12.3.3)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
log_in_file!
|
19
|
+
minitest (~> 5.0)
|
20
|
+
minitest-color
|
21
|
+
rake (~> 12.0)
|
22
|
+
|
23
|
+
BUNDLED WITH
|
24
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# LogInFile
|
2
|
+
|
3
|
+
To write easily log messages in an output file.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'log_in_file'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install log_in_file
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
~~~ruby
|
24
|
+
require 'log_in_file'
|
25
|
+
|
26
|
+
logif("This message will be always written in a file.")
|
27
|
+
|
28
|
+
Logif.severity_level = Logif::WARN|Logif::ERROR
|
29
|
+
|
30
|
+
# This message will not be written
|
31
|
+
logif(Logif::INFO, "Message written only for info")
|
32
|
+
|
33
|
+
# This message will be written
|
34
|
+
logif(Logif::WARN, "Be carefull!")
|
35
|
+
|
36
|
+
~~~
|
37
|
+
|
38
|
+
### Message with data (*template message*)
|
39
|
+
|
40
|
+
~~~ruby
|
41
|
+
|
42
|
+
msg = "This is a %{how} message."
|
43
|
+
logif( msg, **{ data: { how: 'right' } } )
|
44
|
+
|
45
|
+
# => write "This is a right message." in the log file.
|
46
|
+
~~~
|
47
|
+
|
48
|
+
To open the log file (to read it):
|
49
|
+
|
50
|
+
~~~ruby
|
51
|
+
Logif.open
|
52
|
+
|
53
|
+
# or:
|
54
|
+
|
55
|
+
Logif.start
|
56
|
+
~~~
|
57
|
+
|
58
|
+
To remove the log file:
|
59
|
+
|
60
|
+
~~~ruby
|
61
|
+
Logif.remove_log
|
62
|
+
~~~
|
63
|
+
|
64
|
+
|
65
|
+
### Severity levels
|
66
|
+
|
67
|
+
~~~
|
68
|
+
Logif::NOTICE
|
69
|
+
Logif::INFO
|
70
|
+
Logif::WARN
|
71
|
+
Logif::ERROR
|
72
|
+
~~~
|
73
|
+
|
74
|
+
## Development
|
75
|
+
|
76
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
77
|
+
|
78
|
+
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).
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/log_in_file.
|
83
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "log_in_file"
|
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,36 @@
|
|
1
|
+
module Logif
|
2
|
+
NOTICE = 2
|
3
|
+
INFO = 4
|
4
|
+
WARN = 8
|
5
|
+
ERROR = 16
|
6
|
+
ALL = ERROR|WARN|INFO|NOTICE
|
7
|
+
class << self
|
8
|
+
|
9
|
+
|
10
|
+
# @api
|
11
|
+
def severity_level=(value)
|
12
|
+
value.is_a?(Integer) || raise(ArgumentError.new("Logif.severity_level should be a Integer, not a #{value.class}."))
|
13
|
+
@severity_level = value
|
14
|
+
end
|
15
|
+
|
16
|
+
# @api
|
17
|
+
def remove_log
|
18
|
+
LogInFile.remove_log
|
19
|
+
end
|
20
|
+
|
21
|
+
# @api
|
22
|
+
def open
|
23
|
+
LogInFile.open_log
|
24
|
+
end
|
25
|
+
alias :start :open
|
26
|
+
|
27
|
+
def severity?(value)
|
28
|
+
(severity_level & value) > 0
|
29
|
+
end
|
30
|
+
|
31
|
+
def severity_level
|
32
|
+
@severity_level ||= Logif::ALL
|
33
|
+
end
|
34
|
+
|
35
|
+
end #/ << self
|
36
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#
|
2
|
+
# Main method
|
3
|
+
#
|
4
|
+
|
5
|
+
# @api
|
6
|
+
#
|
7
|
+
# = main =
|
8
|
+
#
|
9
|
+
# @param sevevity [Integer] [Optional]
|
10
|
+
# @param message [String] The message to log in the file
|
11
|
+
# @param options [Hash|Nil] Options
|
12
|
+
# :data Data for template message (message with %{...})
|
13
|
+
#
|
14
|
+
def logif(*args)
|
15
|
+
if args[0].is_a?(Integer)
|
16
|
+
severity, message, options = args
|
17
|
+
return unless Logif.severity?(severity)
|
18
|
+
else
|
19
|
+
message, options = args
|
20
|
+
end
|
21
|
+
options ||= {}
|
22
|
+
LogInFile.write(message, **options) # return true if ok
|
23
|
+
end
|
24
|
+
|
25
|
+
module LogInFile
|
26
|
+
class << self
|
27
|
+
|
28
|
+
def write(message, **options)
|
29
|
+
rf.puts(formated_message(message, **options))
|
30
|
+
return true
|
31
|
+
end
|
32
|
+
|
33
|
+
def formated_message(message, **options)
|
34
|
+
s = message.dup
|
35
|
+
s = s % options[:data] if options.key?(:data)
|
36
|
+
s = "--- #{Time.now.strftime(LOG_TIME_FORMAT)} #{s}"
|
37
|
+
return s
|
38
|
+
end
|
39
|
+
|
40
|
+
def open_log
|
41
|
+
close
|
42
|
+
`open "#{LogInFile.logfile_path}"`
|
43
|
+
end
|
44
|
+
|
45
|
+
def rf
|
46
|
+
@rf ||= begin
|
47
|
+
File.open(logfile_path, File::APPEND|File::WRONLY|File::CREAT)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def close
|
52
|
+
@rf ||= nil
|
53
|
+
rf.close unless @rf.nil?
|
54
|
+
@rf = nil
|
55
|
+
end
|
56
|
+
|
57
|
+
def remove_log
|
58
|
+
close
|
59
|
+
File.delete(logfile_path) if File.exist?(logfile_path)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Log file path
|
63
|
+
#
|
64
|
+
def logfile_path
|
65
|
+
@logfile_path ||= File.join(logfile_folder, 'loginfile.log')
|
66
|
+
end
|
67
|
+
|
68
|
+
def logfile_folder
|
69
|
+
@logfile_folder ||= begin
|
70
|
+
if tmp_folder.nil?
|
71
|
+
File.expand_path('.')
|
72
|
+
else
|
73
|
+
tmp_folder
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Maybe a temp folder
|
79
|
+
def tmp_folder
|
80
|
+
@tmp_folder ||= find_temporary_folder
|
81
|
+
end
|
82
|
+
|
83
|
+
def find_temporary_folder
|
84
|
+
curdir = File.expand_path('.')
|
85
|
+
['tmp','temp','temporary','.tmp'].each do |cname|
|
86
|
+
pth = File.join(curdir, cname)
|
87
|
+
return pth if File.exist?(pth)
|
88
|
+
end
|
89
|
+
return nil
|
90
|
+
end
|
91
|
+
|
92
|
+
LOG_TIME_FORMAT = '%Y %m %d %H:%M:%S'
|
93
|
+
end #/ << self
|
94
|
+
end #/ module LogInFile
|
data/lib/log_in_file.rb
ADDED
data/log_in_file.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/log_in_file/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "log_in_file"
|
5
|
+
s.version = LogInFile::VERSION
|
6
|
+
s.authors = ["PhilippePerret"]
|
7
|
+
s.email = ["philippe.perret@yahoo.fr"]
|
8
|
+
|
9
|
+
s.summary = %q{To easily log messages in a file}
|
10
|
+
s.description = %q{Ruby gem to log messages in a output file easily.}
|
11
|
+
s.homepage = "https://rubygems.org/gems/log_in_file"
|
12
|
+
s.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
s.add_development_dependency 'minitest'
|
15
|
+
s.add_development_dependency 'minitest-color'
|
16
|
+
|
17
|
+
s.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
|
+
|
19
|
+
s.metadata["homepage_uri"] = s.homepage
|
20
|
+
s.metadata["source_code_uri"] = "https://github.com/PhilippePerret/gem-log_in_file"
|
21
|
+
s.metadata["changelog_uri"] = "https://github.com/PhilippePerret/gem-log_in_file/CHANGELOG.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|features)/}) }
|
27
|
+
end
|
28
|
+
s.bindir = "exe"
|
29
|
+
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: log_in_file
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- PhilippePerret
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest-color
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Ruby gem to log messages in a output file easily.
|
42
|
+
email:
|
43
|
+
- philippe.perret@yahoo.fr
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- lib/log_in_file.rb
|
58
|
+
- lib/log_in_file/logif_module.rb
|
59
|
+
- lib/log_in_file/main.rb
|
60
|
+
- lib/log_in_file/version.rb
|
61
|
+
- log_in_file.gemspec
|
62
|
+
homepage: https://rubygems.org/gems/log_in_file
|
63
|
+
licenses: []
|
64
|
+
metadata:
|
65
|
+
allowed_push_host: https://rubygems.org
|
66
|
+
homepage_uri: https://rubygems.org/gems/log_in_file
|
67
|
+
source_code_uri: https://github.com/PhilippePerret/gem-log_in_file
|
68
|
+
changelog_uri: https://github.com/PhilippePerret/gem-log_in_file/CHANGELOG.md
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.3.0
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubygems_version: 3.1.6
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: To easily log messages in a file
|
88
|
+
test_files: []
|