hammerspoon icon indicating copy to clipboard operation
hammerspoon copied to clipboard

Feature Request - Option to save Console output to Log Files

Open latenitefilms opened this issue 7 years ago • 7 comments

I reckon it would be handy if Hammerspoon could automatically save the Console output to timestamped files in ~/Library/Logs/Hammerspoon - as this might offer additional help when trying to work out why something might be crashing. Maybe it saves the last 30 sessions in individual files?

You could probably do this as a Spoon - however, I reckon it would be worthwhile building into the Objective-C code, so that it stills saves if the Lua side of things breaks.

Thoughts @asmagill & @cmsj ?

latenitefilms avatar Feb 14 '18 11:02 latenitefilms

I'm going to close this issue for now as it hasn't had any traction.

latenitefilms avatar Mar 02 '19 13:03 latenitefilms

Apologies for bumping such an old issue, but I was surprised to learn that hs.logger doesn’t write to a file by default. I’m surprised that this issue hasn’t had more engagement. I’ll update this thread if I find an easy way to add this functionality.

Haven’t tested yet, but consolepipe seems promising:

https://github.com/asmagill/hammerspoon_asm/tree/master/consolepipe

AdamWagner avatar Oct 16 '20 12:10 AdamWagner

hs.console.getConsole allows you to get the contents of the output in the console which you could then write to a file at intervals with the built in lua io library. (See the hs.styledtext library, specifically hs.styledtext:convert if you want to retrieve the console text with colors and save it as RTF or similar.)

This wouldn't include hs.logger output that was below the threshold for being displayed on the console at the time it was issued, however.

Modifying hs.logger to log to a file or to automatically capture the console output is not a priority at the moment, but would make for a potentially useful Spoon if someone wants to look into it. Coding this in Objective-C might be slightly faster, but wouldn't change what could be captured as all output to the console and all logging through hs.logger is funneled through lua side wrappers anyways... a true application crash would bypass these in any case and we'd still have to rely on information captured by the OS in the application crash log, which we already have in most cases.

asmagill avatar Oct 17 '20 02:10 asmagill

I was looking into this as well and found it's actually quite doable. The global print is overridden by Hammerspoon to write to HS console. You can override print again to add file logging. This is an example:

hammerspoonLogFile = assert(io.open('test.log','a'))
hammerspoonLogFile:setvbuf("line")

-- Override Hammerspoon's print with print that logs to file, not just HS console
-- See print() definition in https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/_coresetup/init.lua
local old_print, tostring = print, tostring
local tconcat, pack = table.concat, table.pack
print = function(...)
    local vals = pack(...)

    for k = 1, vals.n do
      vals[k] = tostring(vals[k])
    end

    local l = tconcat(vals, "\t")
    hammerspoonLogFile:write(l, '\n')
    return old_print(l)
end

Edit: I was thinking about hs.logger should allow file logging. The above solution also log all hs.logger log to file. It's actually nicer if hs.logger provides a hook in hs.logger (at this line https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/logger/init.lua#L185) to allow override the default handling of a log. Preferably, the hook should be a callback to handle a log record ( the table {time=ct,level=lvl,id=id,message=msg} at line 177 ) instead of a string because the overriding handler could do their own formatting.

It's probably not a high priority (the same reason this request was closed), so the above solution should work for now.

gineer01 avatar Nov 01 '20 06:11 gineer01

Thanks for the "how-to" @gineer01! I also agree that it'd be preferable for hs.logger to allow overriding the log handler.

AdamWagner avatar Nov 07 '20 17:11 AdamWagner

I wonder if we could use CocoaLumberjack for this?

latenitefilms avatar Aug 06 '24 16:08 latenitefilms

@latenitefilms I think we probably need to completely rework how logging happens in HS. It's currently a weird convoluted mess that exists in both Lua and ObjC, for no obvious reason. It would probably make sense to gather all of our requirements for logging in one place, so we can design something that fits.

cmsj avatar Aug 06 '24 22:08 cmsj