hvelarde
hvelarde

Reputation: 2876

How to get the location of a Zope installation from inside an instance?

We are working on an add-on that writes to a log file and we need to figure out where the default var/log directory is located (the value of the ${buildout:directory} variable).

Is there an easy way to accomplish this?

Upvotes: 2

Views: 343

Answers (2)

maurits
maurits

Reputation: 2365

You could let buildout set it in parts/instance/etc/zope.conf in an environment variable:

[instance]
recipe = plone.recipe.zope2instance
environment-vars =
    BUILDOUT_DIRECTORY ${buildout:directory}

Check it in Python code with:

import os
buildout_directory = os.environ.get('BUILDOUT_DIRECTORY', '')

By default you already have the INSTANCE_HOME environment variable, which might be enough.

Upvotes: 2

alepisa
alepisa

Reputation: 1324

In the past I had a similar use case. I solved it by declaring the path inside the zope.conf:

zope-conf-additional +=
  <product-config pd.prenotazioni>
    logfile ${buildout:directory}/var/log/prenotazioni.log
  </product-config>

See the README of this product:

This zope configuration can then be interpreted with this code:

from App.config import getConfiguration

product_config = getattr(getConfiguration(), 'product_config', {})
config = product_config.get('pd.prenotazioni', {})
logfile = config.get('logfile')

See the full example

Worth noting is the fact that the initial return avoids multiple logging if the init function is mistakenly called more than once.

Anyway, if you do not want to play with buildout and custom zope configuration, you may want to get the default event log location.

It is specified in the zope.conf. You should have something like this:

<eventlog>
  level INFO
  <logfile>
    path /path/to/plone/var/log/instance.log
    level INFO
  </logfile>
</eventlog>

I was able to obtain the path with this code:

from App.config import getConfiguration 
import os

eventlog = getConfiguration().eventlog
logpath = eventlog.handler_factories[0].instance.baseFilename
logfolder = os.path.split(logpath)[0] 

Probably looking at in the App module code you will find a more straightforward way of getting this value.

Another possible (IMHO weaker) solution would be store (through buildout or your prefered method) the logfile path into an environment variable.

Upvotes: 3

Related Questions