I wrote a script that operates on my Mac just fine. It has this line of code in it:
我写了一个在Mac上运行的脚本就好了。它有这行代码:
filename = "2011"
File.open(filename, File::WRONLY|File::CREAT|File::EXCL) do |logfile|
logfile.puts "MemberID,FirstName,LastName,BadEmail,gender,dateofbirth,ActiveStatus,Phone"
On Windows the script runs fine and it creates the logfile 2011
, but it doesn't actually puts
anything to that logfile, so the file is created, the script runs, but the logging doesn't happen.
在Windows上,脚本运行正常,它会创建日志文件2011,但它实际上并没有向该日志文件添加任何内容,因此创建了文件,脚本运行,但日志文件不会发生。
Does anyone know why? I can't think of what would have changed in the actual functionality of the script that would cause the logging to cease.
有谁知道为什么?我想不出脚本的实际功能会发生什么变化会导致日志记录停止。
1 个解决方案
#1
1
First, for clarity I wouldn't use the flags to specify how to open/create the file. I'd use:
首先,为清楚起见,我不会使用标志来指定如何打开/创建文件。我用的是:
File.open(filename, 'a')
That's the standard mode for log-files; You want to create it if it doesn't exist, and you want to append if it does.
这是日志文件的标准模式;如果它不存在你想要创建它,如果它存在你想要追加它。
Logging typically requires writing to the same file multiple times through the running time of an application. People like to open the log and leave it open, but there's potential for problems if the code crashes before the file is closed or it gets flushed by Ruby or the OS. Also, the built-in buffering by Ruby and the OS can cause the file to buffer, then flush, which, when you're tailing the file, will make it jump in big chunks, which isn't much good if you're watching for something.
日志记录通常需要在应用程序的运行时间内多次写入同一文件。人们喜欢打开日志并保持打开,但如果代码在文件关闭之前崩溃或者被Ruby或操作系统刷新,则可能会出现问题。此外,Ruby和操作系统的内置缓冲可以导致文件缓冲,然后刷新,当你拖尾文件时,它会使它跳进大块,如果你是这样的话,这不是很好看某事。
You can tell Ruby to force flushing immediately when you write to the file by setting sync = true
:
您可以通过设置sync = true告诉Ruby在写入文件时立即强制刷新:
logfile = File.open(filename, 'a')
logfile.sync = true
logfile.puts 'foo'
logfile.close
You could use fsync
, which also forces the OS to flush its buffer.
您可以使用fsync,这也会强制操作系统刷新其缓冲区。
The downside to forcing sync in either way is you negate the advantage of buffering your I/O. For normal file writing, like to a text file, don't use sync
because you'll slow your application down. Instead let normal I/O happen as Ruby and the OS want. But for logging it's acceptable because logging should periodically send a line, not a big blob of text.
以任何一种方式强制同步的缺点是你否定了缓冲I / O的优势。对于正常的文件写入,比如文本文件,不要使用同步,因为你会减慢你的应用程序速度。相反,正常的I / O就像Ruby和操作系统想要的那样发生。但是对于日志记录它是可以接受的,因为日志记录应该定期发送一行,而不是大量的文本。
You could immediately flush
the output, but that gets redundant and violates the DRY principle:
您可以立即刷新输出,但这会多余并违反DRY原则:
logfile = File.open(filename, 'a')
logfile.puts 'foo'
logfile.flush
logfile.puts 'bar'
logfile.flush
logfile.close
close
flushes before actually closing the file I/O.
在实际关闭文件I / O之前关闭刷新。
You can wrap your logging output in a method:
您可以将日志记录输出包装在方法中:
def log(text)
File.open(log_file, 'a') do |logout|
logout.puts(text)
end
end
That'll open, then close, the log file, and automatically flush the buffer, and negate the need to use sync
.
这将打开然后关闭日志文件,并自动刷新缓冲区,并且无需使用同步。
Or you could take advantage of Ruby's Logger class and let it do all the work for you.
或者您可以利用Ruby的Logger类,让它为您完成所有工作。
#1
1
First, for clarity I wouldn't use the flags to specify how to open/create the file. I'd use:
首先,为清楚起见,我不会使用标志来指定如何打开/创建文件。我用的是:
File.open(filename, 'a')
That's the standard mode for log-files; You want to create it if it doesn't exist, and you want to append if it does.
这是日志文件的标准模式;如果它不存在你想要创建它,如果它存在你想要追加它。
Logging typically requires writing to the same file multiple times through the running time of an application. People like to open the log and leave it open, but there's potential for problems if the code crashes before the file is closed or it gets flushed by Ruby or the OS. Also, the built-in buffering by Ruby and the OS can cause the file to buffer, then flush, which, when you're tailing the file, will make it jump in big chunks, which isn't much good if you're watching for something.
日志记录通常需要在应用程序的运行时间内多次写入同一文件。人们喜欢打开日志并保持打开,但如果代码在文件关闭之前崩溃或者被Ruby或操作系统刷新,则可能会出现问题。此外,Ruby和操作系统的内置缓冲可以导致文件缓冲,然后刷新,当你拖尾文件时,它会使它跳进大块,如果你是这样的话,这不是很好看某事。
You can tell Ruby to force flushing immediately when you write to the file by setting sync = true
:
您可以通过设置sync = true告诉Ruby在写入文件时立即强制刷新:
logfile = File.open(filename, 'a')
logfile.sync = true
logfile.puts 'foo'
logfile.close
You could use fsync
, which also forces the OS to flush its buffer.
您可以使用fsync,这也会强制操作系统刷新其缓冲区。
The downside to forcing sync in either way is you negate the advantage of buffering your I/O. For normal file writing, like to a text file, don't use sync
because you'll slow your application down. Instead let normal I/O happen as Ruby and the OS want. But for logging it's acceptable because logging should periodically send a line, not a big blob of text.
以任何一种方式强制同步的缺点是你否定了缓冲I / O的优势。对于正常的文件写入,比如文本文件,不要使用同步,因为你会减慢你的应用程序速度。相反,正常的I / O就像Ruby和操作系统想要的那样发生。但是对于日志记录它是可以接受的,因为日志记录应该定期发送一行,而不是大量的文本。
You could immediately flush
the output, but that gets redundant and violates the DRY principle:
您可以立即刷新输出,但这会多余并违反DRY原则:
logfile = File.open(filename, 'a')
logfile.puts 'foo'
logfile.flush
logfile.puts 'bar'
logfile.flush
logfile.close
close
flushes before actually closing the file I/O.
在实际关闭文件I / O之前关闭刷新。
You can wrap your logging output in a method:
您可以将日志记录输出包装在方法中:
def log(text)
File.open(log_file, 'a') do |logout|
logout.puts(text)
end
end
That'll open, then close, the log file, and automatically flush the buffer, and negate the need to use sync
.
这将打开然后关闭日志文件,并自动刷新缓冲区,并且无需使用同步。
Or you could take advantage of Ruby's Logger class and let it do all the work for you.
或者您可以利用Ruby的Logger类,让它为您完成所有工作。