Git Product home page Git Product logo

tail_from_sentinel's Introduction

TailFromSentinel

Parses a file, looking for a sentinel line that satisfies a block, and returns
all lines from there until the end of the file (including the sentinel itself).

This can be handy when you are looking for information in a log file only after
a certain condition (eg. a date) has been met.

You can either use it on it's own, or subclass it to build your own custom
tail-like classes.

Standalone:
  File.open('/var/log/messages', 'r') do |file|
    tfs=TailFromSentinel::Base.new(file) { |line| line =~ /Dec 20 11:2\d+/ }
  end
  tfs.data # => [ 'Dec 20 11:20:00 ...', 'Dec 20 11:21:00 ...', ... ]

Subclassed:
  class TailMyFile < TailFromSentinel::Base
    DATA_ROOT="/path/to/data/"

    def initialize(data_file)
      file=File.open(File.join(DATA_ROOT, data_file), 'r') do |file|
        super(file, &select_sentinel_criteria(data_file))
      end
    end

    def select_sentinel_criteria(data_file)
      case data_file
        when ''production.log'
          Proc.new { |line| line =~ /pattern/ }
        when 'development.log'
          Proc.new { |line| line =~ /another pattern/ }
        else
          raise "Unhandled file: #{data_file}"
      end
    end
  end

  t=TailMyFile.new('production.log')
  t.data # => [ 'Started GET "/pattern ...", "  Processing ... ", ... ]

tail_from_sentinel's People

Contributors

jstirk avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.