log_packer 0.0.1

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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDBjMjY0YzNhZDU1ODE1NGQ2NTA5YzY1ZGM2ZjA0YmQwNDVlNWViMA==
5
+ data.tar.gz: !binary |-
6
+ MDY3OTU2MjZlNjE1ZTJhMzY1ZmE1YzRlYTAxYjIwN2Y1YTg5NjFjNg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YTM2N2JmYzdkMDQxMjQ4NTJmZDFiNDUyMWVhOTQyOTUxMjMzNmU4MWQ4N2Jj
10
+ M2I5NmE1M2EzNTdmMGM5ZDE2M2FkMWY0NTljYjU2OWRmYTBjZTcwZmMzMGFj
11
+ NmE0MzRlNDk2MWM0N2Q2YTYxZjhmYzhmMGZkMDQyMDNlZmU3MGE=
12
+ data.tar.gz: !binary |-
13
+ YzljYjQ5NWQyYWZjOWI4M2FiNmE0MzdjNTVmOGFhYzJhM2E4YzZkN2U3YmE4
14
+ Nzg5ZjZlM2M5ZjRhM2FkMjkyMWVkNDU0Mzk1MDQzMmZhZjQ1ZDhkNGIyYTMx
15
+ ZGYwZmY0ZTJjZmFmM2E4NDQ5NDVkMjQ2NDE1MjFmNzhiNTBjOWE=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in log_packer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 yakjuly
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ #requirement
2
+ Install bzip2
3
+
4
+
5
+ # How to use
6
+ ```
7
+ #config/initializers/log_packer.rb
8
+
9
+ LogPacker.log_dirs = [
10
+ Rails.root.join("log").to_s
11
+ ]
12
+
13
+ LogPacker.log_filenames = [
14
+ Rails.root.join("log/production.log"),
15
+ Rails.root.join("log/restclient.log")
16
+ ]
17
+
18
+ rake log_packer:archive_log
19
+ ```
20
+
21
+ bzcat log/last/production.log.20140606095739.bz2 | less
22
+
23
+ #with Whenever
24
+ ```
25
+ every :day, at: "1:00 am" do
26
+ rake "log_packer:archive"
27
+ end
28
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/log_packer.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "log_packer/version"
2
+ require 'log_packer/packer'
3
+
4
+ require "log_packer/railtie" if defined?(Rails)
5
+
6
+ module LogPacker
7
+ # RAILS_ROOT/log/*.log
8
+ #
9
+ @@log_filenames = []
10
+ mattr_accessor :log_filenames
11
+
12
+
13
+ @@log_dirs = []
14
+ mattr_accessor :log_dirs
15
+
16
+ end
@@ -0,0 +1,44 @@
1
+ require "fileutils"
2
+
3
+ module LogPacker
4
+ class Packer
5
+
6
+ class << self
7
+ # Functions
8
+ def truncate(file)
9
+ File.open(file, 'w') { |f| f.write '' }
10
+ end
11
+
12
+ def archive_path(logfile)
13
+ archived_dir = File.dirname(logfile) + '/archived'
14
+ last_dir = File.dirname(logfile) + '/last'
15
+ FileUtils.mkdir(archived_dir) unless File.directory?(archived_dir)
16
+ FileUtils.mkdir(last_dir) unless File.directory?(last_dir)
17
+ archived_dir + '/' + File.basename(logfile) + '.' + Time.now.strftime('%Y%m%d%H%M%S')
18
+ end
19
+
20
+ def last_dir(logfile)
21
+ log_path = File.directory?(logfile) ? logfile : File.dirname(logfile)
22
+ last_dir = log_path + '/last/'
23
+ FileUtils.mkdir(last_dir) unless File.directory?(last_dir)
24
+ last_dir
25
+ end
26
+
27
+ def rotate(logfile)
28
+ if !File.exists?(logfile)
29
+ puts "#{logfile} does not exist"
30
+ elsif File.size(logfile) == 0
31
+ puts "#{logfile} is empty"
32
+ else
33
+ archive_file = archive_path(logfile)
34
+ last_dir = last_dir(logfile)
35
+ puts "Copying #{logfile} to #{archive_file}.bz2"
36
+ FileUtil.cp logfile, "#{archive_file}"
37
+ truncate logfile
38
+ system "bzip2 #{archive_file}"
39
+ system "cd #{last_dir} && ln -sf #{archive_file}.bz2 ."
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ require 'log_packer'
2
+ require 'rails'
3
+
4
+ module LogPacker
5
+ class Railtie < Rails::Railtie
6
+
7
+ rake_tasks do
8
+ load File.expand_path('../tasks.rb', __FILE__)
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ namespace :log_packer do
2
+
3
+ desc "Archive Log by configuration LogPacker.log_path = [ Rails.root.join('log/*.log') ]"
4
+ task :archive => :environment do
5
+
6
+ puts "==========================================="
7
+ puts " Logrotate starting at #{Time.now.strftime '%Y-%m-%d %H:%M:%S'}"
8
+ puts "==========================================="
9
+
10
+ log_filenames = LogPacker.log_filenames.map(&:to_s)
11
+ log_dirs = LogPacker.log_dirs
12
+
13
+
14
+ total_filenames = (Dir[*log_dirs] + log_filenames).uniq
15
+ parent_dir_names = total_filenames.map{|filename| File.basename(filename) }.uniq
16
+
17
+ #clear /last
18
+ parent_dir_names.each do |dirname|
19
+ PackLoger.last_dir(dirname)
20
+ system "cd #{dirname} && rm -f *.bz2"
21
+ end
22
+
23
+ total_filenames.each do |logfile|
24
+ puts "Rotating: #{logfile}"
25
+ LogPacker::Packer.rotate logfile
26
+ end
27
+
28
+ puts "==========================================="
29
+ puts " Logrotate stopted at #{Time.now.strftime '%Y-%m-%d %H:%M:%S'}"
30
+ puts "==========================================="
31
+ puts ""
32
+
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module LogPacker
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'log_packer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "log_packer"
8
+ spec.version = LogPacker::VERSION
9
+ spec.authors = ["yakjuly"]
10
+ spec.email = ["yakjuly@gmail.com"]
11
+ spec.description = %q{Log Packer is a tool to package rails log to log/archive folder}
12
+ spec.summary = %q{Ususally work with whenever to archive log every night}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log_packer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - yakjuly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-06 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: Log Packer is a tool to package rails log to log/archive folder
42
+ email:
43
+ - yakjuly@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/log_packer.rb
54
+ - lib/log_packer/packer.rb
55
+ - lib/log_packer/railtie.rb
56
+ - lib/log_packer/tasks.rb
57
+ - lib/log_packer/version.rb
58
+ - log_packer.gemspec
59
+ homepage: ''
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Ususally work with whenever to archive log every night
83
+ test_files: []