Archive for April 1st, 2012

Clearing Lightroom plugin log files automatically

(Note: This is a copy of my article on Adobe’s Lightroom Developer Connection. Just in case anything happens to it.)

Problem

Logfiles grow over time if you don’t manage them, but Lightroom doesn’t provide a way to automatically delete the generated logfiles. We could manage the log ourselves, but we can’t tell Lightroom to log to a particular file.

Solution

We know that the log is always created in the user’s documents folder. Thus, we can use LrPathUtils.getStandardFilePath(‘documents’) to get a path to that folder. We also know the name of the file and the extension, so we can build a full filepath that we can use to manage the logfile.

Detailed explanation

Using a logfile for logging a plugin is a simple way for tracing errors during development (arguably simpler than getting a console set up), and it’s simple for end users to send to you when reporting bugs.

However, Lightroom will constantly append to a logfile, so entries will get stale, and the file will grow as your plugin is used. So, we need to manage the logfile somehow.

The easiest solution is to simply delete any existing logfile on every (re)load of your plugin. The kink in this plan is that there is no way (at least, not documented in the API reference) to tell Lightroom to log to a specific file.

However, we know that Lightroom will create a log in the user’s Documents folder, and use the logger name for the filename, and use the file suffix “.log”. With this information, we can build a path using LrPathUtils functions that allows us to get a proper filepath, one that can be passed to LrFileUtils.delete().

Now that we have a path to the log, we can do whatever we want with it. It’s a simple matter to add code that will delete the logfile in your plugin initialization script.

As an example, this is what I’m currently using in my plugin:

01 local logger = import 'LrLogger'( 'Stash' )
02 local LrFileUtils = import 'LrFileUtils'
03 local LrPathUtils = import 'LrPathUtils'
04
05 local logPath = LrPathUtils.child(LrPathUtils.getStandardFilePath('documents'), "Stash.log")
06 if LrFileUtils.exists( logPath ) then
07 local success, reason = LrFileUtils.delete( logPath )
08 if not success then
09 logger:error("Error deleting existing logfile!" .. reason)
10 end
11 end

(If you want, you can see the code in context of the full plugin here: github.com/kyl191/lr-stash/blob/master/LrInitPlugin.lua)

,

No Comments