I've been appending to an open file, and note that...
# lucee
a
I've been appending to an open file, and note that Lucee seems to only actually be writing to the file system every 7kB or so? Any reason for this? Any way to get it to do what it's told, when it's told? ie: if I append a single byte: append the single byte to the file now.
z
that's the operating system
a
So the underlying file stream (or whatever the correct techo term is) holds a buffer in memory and only flushes to the actual file when it feels like it?
z
quite often yes, are you using filewrite?
a
yeah
Copy code
fh = fileOpen(somePath, "append")
// later
fileWrite(fh, "some stuff")
(very pseudocode)
if I close the file it seems to flush
The thing is I am trying to avoid opening and closing the file.
z
fileOpen, i.e get a handle is going to do that
is my rough menttal model
a
When Lucee does its own logging, does it just do the equiv of
fileWrite(somePath, someStuff)
, with a file-open, file-write, file-close?
Otherwise I'd expect to see pretty much the same behaviour with Lucee. But I see every log line written as I log it.
z
no that's log4j with it's own appender for each log
a
Hence my usage of the word "equiv"
ie: via whatever higher-level mechanism is being used to do so, it's opening / writing / closing the file, rather than opening, appending, [maybe closing later, who knows]
Or at Java level is there an option to force a flush that is not available to me in CFML?
I'd be really surprised if Log4J was opening and closing the file with every line-write.
z
fh.getResource().getOutputStream().flush()
might work?
a
Aah! I was just hunting around in that object 😄
(amongst testing what CF does with the same code)
I'm afraid to say that CF behaves as I would expect. Each line is written to the file as I append it. So pretty sure this is a Lucee thing, not an OS thing.
z
so fh.getResource().getOutputStream().flush() didn't work?
a
ah... nope
sorry was gonna check more thoroughly, but then I got sidetracked and decided to post on the other thing instead
I think it didn't do anything at all. Let me recheck...
z
with your example, can you wrap it in a cftimer type="outline" and try it with a fileAppend, that buffering is all about performance and a typical pattern in java
a
cftimer
?? What voodoo is that?
(I mean, I know what
<cftimer>
is... how does this figure in any of this?)
Oh the flush thing just meant nothing ever flushed at all. IE: nothing ever got written to the file. Worth a crack though!
Oh sorry, were you wanting to just gather some metrics with the timer suggestion? Not as a solution.
z
my reasoning being, your are using a file handle for performance reasons (trying to roll your own logging right) and I'm wonder what the difference in perf between not using a file handle and using one is, (and then with flushing)
a
Gotcha, yeah. Well given I did not anticipate getting a timely answer for the Q on dev.lucee.org I've already reverted the code to using fileappend anyhow. I'd rather it be slow and actually work, than being faster to not work 😉