Corona Advanced Logging

Hi,

I would like to present my first public Corona / Lua module. Looking forward to your feedback and suggestions.

Repository: https://github.com/promptcode/CoronaAdvancedLogging

Used for error handling and logging events to console and a set of rolling text log files.

  • Use it instead of print in your Corona project
  • Your messages will be printed in console but also logged in text files
  • Every message in log file contains time, log level and caller trace (module name and line number)
  • A set of rolling text files is used so you can retrace the steps that lead to a bug
  • Runtime errors get logged automatically (including stacktrace)
  • Users get automatically prompt on runtime errors and can report them via email
  • Application administrator receives device / platform info and log files in attachment

build.settings  - required for better debugging on device

[lua]settings = {

    build = {

          neverStripDebugInfo = true

    }

}[/lua]

sample.lua  - you can test this is your main function or download sample project from GitHub

[lua]


– REQUIRE MODULES


local sqlite3 = require(“sqlite3”);

local log = require(“log”)


– SETUP DATABASE CONNECTION

– Required to use Advanced logging module


local path = system.pathForFile(“sample.db”, system.DocumentsDirectory)

db = sqlite3.open(path)  


– SETUP ADVANCED LOGGING MODULE

– This is the simple setup, see properties and defaults


log:set(db, “your_email_adress@gmail.com”)


– TEST ADVANCED LOGGING MODULE


– Log your events, use log instead of print

log:log(“Advanced logging module is now ready”)

– Here we make an intentional error to test error reporting

asd = asd … “asd”

[/lua]

log properties  - customize log module

[lua]-- Prefix for log files

log.fileNamePrefix = “log”

– Directory where to save log files

log.directory = system.DocumentsDirectory

– Maximum number of log files

log.numberOfRollingFiles = 6

– Set to true if you want to trace module name and line number on each info message

log.debugCalls = true

– Maximum depth for caller traceing

log.debugCallDepth = 4

– Maximum log file size in Bytes

log.maxFileSizeInBytes = 20480

– Database table name where to save log parameters

log.tableName = “log_params”

– Set to true if you want an alert to popup on runtime errors

log.alertErrors = true

– Title for runtime errors alert

log.alertTitle = “Oops, an error occurred”

– Text for runtime errors alert

log.alertText = “Please report this error to administrator on email:\n\n”

– 1st button for runtime errors alert label

log.alertButtonReport = “Report on email”

– 2nd button for runtime errors alert label

log.alertButtonDismiss = “Dismiss”

– Email adress on which to send error reports

log.alertEmail = “”

– Erorr report email subject

log.emailSubject = “Error report”

– Erorr report email text before device / platform into

log.emailPreText = "Hi\n\nI want to report an error in application. " …

                   “Logs files are attached. Here is my device info: \n”

– Erorr report email text after device / platform into

log.emailPostText = “\n\n Thank you.”[/lua]

Here is an example of log file contents.

ss0.png

Runtime errors get automaticly logged and user gets prompt to report error to administrator via email.

ss1.png

Administrator receives an email containing device / platform info and log files.

ss2.png

Regards,

Daniel

Thank you very much for your contribution. This will be a very useful library especially when working with Corona Viewer.

Your welcome. I have updated the module for better debugging on device (thanks to Perry Clark).

Corona Viewer does not yet support _ unhandledError  _listener, but I hope that won’t be the case for long.

Thanks for sharing this with the community.  I think folks will find this very useful.  I especially like your bug mailer.

Can I add a suggestion?  You can make your log() method take multiple arguments by changing the old log() function name to logi(), and then by doing this:

local log:log( ... ) local sep = log.sep or " " local message = tostring(arg[1]) for i = 2, #arg do message = message .. sep .. tostring(arg[i]) end self:logi(message) -- Call your original logger here end

This test,

log.sep = " " test("Thanks", "for", "the", "advanced", "logger", "it's", string.format("%X", 49153) )

prints something like:

18.08.2014 08:45:15 - INFO - Thanks for the advanced logger it’s C001

Wow, this looks wonderful, thanks god Rob announced it in the From the Forum Issue #33.

I’ll give it a try, thanks for sharing!

Thanks for your feedback, I’m glad you folks like my library and hope it helps you with on device debugging.

@roaminggamer

Your suggestion is noted and included in latest update with only a small change. More here: http://lua-users.org/wiki/TrailingNilParameters

[lua]

function log:log( … )

    local message = “”

    local numOfArguments = arg.n --OK

    --local numOfArguments = #arg --NOT OK, because log:log(nil) would not be logged in this case

    --TODO

end

[/lua]

A separator paramater has been added. Thanks for your suggestion.

Daniel

Thank you very much for your contribution. This will be a very useful library especially when working with Corona Viewer.

Your welcome. I have updated the module for better debugging on device (thanks to Perry Clark).

Corona Viewer does not yet support _ unhandledError  _listener, but I hope that won’t be the case for long.

Thanks for sharing this with the community.  I think folks will find this very useful.  I especially like your bug mailer.

Can I add a suggestion?  You can make your log() method take multiple arguments by changing the old log() function name to logi(), and then by doing this:

local log:log( ... ) local sep = log.sep or " " local message = tostring(arg[1]) for i = 2, #arg do message = message .. sep .. tostring(arg[i]) end self:logi(message) -- Call your original logger here end

This test,

log.sep = " " test("Thanks", "for", "the", "advanced", "logger", "it's", string.format("%X", 49153) )

prints something like:

18.08.2014 08:45:15 - INFO - Thanks for the advanced logger it’s C001

Wow, this looks wonderful, thanks god Rob announced it in the From the Forum Issue #33.

I’ll give it a try, thanks for sharing!

Thanks for your feedback, I’m glad you folks like my library and hope it helps you with on device debugging.

@roaminggamer

Your suggestion is noted and included in latest update with only a small change. More here: http://lua-users.org/wiki/TrailingNilParameters

[lua]

function log:log( … )

    local message = “”

    local numOfArguments = arg.n --OK

    --local numOfArguments = #arg --NOT OK, because log:log(nil) would not be logged in this case

    --TODO

end

[/lua]

A separator paramater has been added. Thanks for your suggestion.

Daniel