Class: YJCocoa::Log

Inherits:
Command
  • Object
show all
Defined in:
lib/yjcocoa/log/log.rb

Overview

Usage

Constant Summary collapse

LOG_FILE =
"YJCocoa.log"

Constants inherited from Command

Command::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#askWithAnswers

Constructor Details

#initialize(argv) ⇒ Log

初始化



35
36
37
38
39
40
# File 'lib/yjcocoa/log/log.rb', line 35

def initialize(argv)
    super
    self.dir = argv.option('dir')
    self.match = argv.option('match')
    self.match = self.match.split(",").reject {|i| i.empty? } if self.match
end

Instance Attribute Details

#dirObject

文件夹地址



31
32
33
# File 'lib/yjcocoa/log/log.rb', line 31

def dir
  @dir
end

#matchObject

匹配的字符串



32
33
34
# File 'lib/yjcocoa/log/log.rb', line 32

def match
  @match
end

Class Method Details

.optionsObject



25
26
27
# File 'lib/yjcocoa/log/log.rb', line 25

def self.options
    [['--dir', '文件夹地址'], ['--match', '匹配的字符串']] + super
end

Instance Method Details

#check(line) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/yjcocoa/log/log.rb', line 75

def check (line)
    self.match.each { |m|
        if line.include?(m)
            return true
        end
    }
    return false
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/yjcocoa/log/log.rb', line 55

def run
    Dir.chdir(self.dir) {
        log = ""
        File.delete(LOG_FILE) if File.exist?(LOG_FILE)
        for path in Dir["**/*.log"].sort do
            puts "解析日志 #{path}".green
            File.open(path, "r") { |file|
                while line = file.gets
                    if self.check(line)
                        log << line
                    end
                end
            }
        end
        puts "\n解析完毕,打开日志:#{dir}/#{LOG_FILE}".green
        File.write(LOG_FILE, log)
        `open #{LOG_FILE}`
    }
end

#validate!Object

businrss



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yjcocoa/log/log.rb', line 43

def validate!
    super
    unless self.dir && Dir.exist?(self.dir)
        puts "dir:#{self.dir} 文件夹不存在".red
        self.banner!
    end
    unless self.match && self.match.length > 0
        puts "match 为空".red
        self.banner!
    end
end