module Gitlab
  class Logger < ::Logger
    def self.file_name
      file_name_noext + '.log'
    end

    def self.error(message)
      build.error(message)
    end

    def self.info(message)
      build.info(message)
    end

    def self.read_latest
      path = Rails.root.join("log", file_name)
      self.build unless File.exist?(path)
      tail_output, _ = Gitlab::Popen.popen(%W(tail -n 2000 #{path}))
      tail_output.split("\n")
    end

    def self.read_latest_for(filename)
      path = Rails.root.join("log", filename)
      tail_output, _ = Gitlab::Popen.popen(%W(tail -n 2000 #{path}))
      tail_output.split("\n")
    end

    def self.build
      new(Rails.root.join("log", file_name))
    end
  end
end

module Gitlab
  class Logger < ::Logger
    def self.file_name
      file_name_noext + '.log'
    end

    def self.error(message)
      build.error(message)
    end

    def self.info(message)
      build.info(message)
    end

    def self.read_latest
      path = Rails.root.join("log", file_name)

      return [] unless File.readable?(path)

      tail_output, _ = Gitlab::Popen.popen(%W(tail -n 2000 #{path}))
      tail_output.split("\n")
    end

    def self.build
      new(Rails.root.join("log", file_name))
    end
  end
end

Fix a potential timeout in `Gitlab::Logger.read_latest`
Fix a potential timeout in `Gitlab::Logger.read_latest`

If this method was called for a file that didn't exist, we attempted to
first `build` it. But if the file wasn't writeable, such as a symlink
pointing to an unmounted filesystem, the method would just hang and
eventually timeout.

Further, this was entirely pointless since we were creating a file and
then shelling out to `tail`, eventually returning an empty Array. Now we
just skip building it and return the empty Array straight away.
