Skip to content

Commit a601208

Browse files
committed
(maint) Stop invalid debug logs from crashing services
Previously if an invalid logfile location was specified e.g. C:\ or a missing parent directory, the service (Language or Debug Server) would error. This is undesired as a bad log file should not cause a catastrophic failure. This commit modifies the logging so that an error occurs when the log file is created it, the error is sent to STDERR, and service continues but with logging disabled. This commit also flushes the log file on each write if needed. This is required as OS buffering makes real time debugging very difficult.
1 parent f0f33c1 commit a601208

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
88

99
- ([GH-11](https://github.com/lingua-pupuli/puppet-editor-services/issues/11)) Refactor the transport layers to loosen object coupling
1010
- ([GH-11](https://github.com/lingua-pupuli/puppet-editor-services/issues/11)) Fix STDIO server
11+
- Stop bad logfile destinations from crashing the language and debug servers
1112

1213
## 0.10.0 - 2018-03-29
1314

lib/puppet-editor-services/logging.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,28 @@ def self.log_message(severity, message)
1616
else
1717
@logger.unknown(message)
1818
end
19+
@log_file.fsync unless @log_file.nil?
1920
end
2021

2122
def self.init_logging(options)
23+
@log_file = nil
2224
if options[:debug].nil?
2325
@logger = nil
2426
elsif (options[:debug].casecmp 'stdout').zero?
2527
@logger = Logger.new($stdout)
2628
elsif !options[:debug].to_s.empty?
2729
# Log to file
28-
@logger = Logger.new(options[:debug])
30+
begin
31+
@log_file = File.open(options[:debug], 'w')
32+
rescue Errno::ENOENT => e
33+
# We can't open the log file and we can't log to STDOUT if we're in STDIO mode
34+
# So log the error to STDERR and disable logging
35+
$stderr.puts "Error opening log file #{options[:debug]} : #{e}" # rubocop:disable Style/StderrPuts
36+
@log_file = nil
37+
return
38+
end
39+
@log_file.sync = true
40+
@logger = Logger.new(@log_file)
2941
end
3042
end
3143
end

0 commit comments

Comments
 (0)