Kernel.at_exit will not “catch”
rescue clause can trap exceptions. By default,
rescue clauses trap
StandardError and subclass exceptions only. A
SystemExit exception is a direct subclass of
Exception, so is not by default trapped by
rescue clauses that do not specify a class trapping list.
(3) In fact someone else’s
rescue clause might trap exceptions before you can, if their code does not re-raise the exception.
(4) So, it would be best if you used a
begin ... rescue ... else ... ensure block around your plugin command(s). If you have multiple commands, they each can call common error handling or logging methods elsewhere in your code. (This is a good job for a library that more than one of your plugins could use.)
# command code
rescue ScriptError, StandardError, SystemExit, SystemStackError => e
raise # re-raise it for other debug utilities, etc.
So, back to specifically logging exceptions.
Have you looked at using (or mimicking) the standard library Logger ?
The code may give you ideas.
I often create custom exception classes within my plugin sub-modules that only my plugin will ever trap. IE:
GoofyError = Class::new(Exception)
# command code
# somewhere here I detect a situation
# and explicitly raise a GoofyError:
fail(GoofyError,"a goofy situation occured",caller) if @situation == true
# more code if @situation != true
rescue GoofyError => e
# recover from GoofyError
rescue => e
# recover from StandardError and it's subclasses