Troubleshooting: Geodjango’s HAS_GDAL is False in OSX
Troubleshooting guide when django.contrib.gis.gdal.HAS_GDAL is False.
Environment
- Mac OSX Lion
- GDAL 1.10 (by using Kyng Chaos’s package.
- Django 1.5
- Python 2.7
osgeo.gdal
First, I realized the python can’t use the gdal in my virtualenv. Fixed it, refferencing this article.
Then, the gdal can be imported. but, HAS_GDAL was still False.
Specifying GDAL library
I noticed the django did’nt know the GDAL library path. It expected in ‘/user/local/lib/libgdal.dylib’. but, in my environment, It is in ‘/Library/Frameworks/GDAL.framework/unix/lib/libgdal.dylib’.
To tell this path, we can set the GDAL_LIBRARY_PATH settings to django.
- GDAL_LIBRARY_PATH = ‘/Library/Frameworks/GDAL.framework/unix/lib/libgdal.dylib’
After this setting, gdal.HAS_GDAL will be True. You should referrence this documentation too.
Don’t format strings before logging in python
You should not provide formatted string to loggers in Python, like this:
logger.info('Logged in: %s' % username)
You should write like this:
logger.info('Logged in: %s', username)
Why?
In mont cases, it is the same as a result. But, internally, the later one contains more information which part of this string represents a username.
You shoud realize the first argument (message) can also be used as a signature.
If you want to aggregate logs, you will group logs by messages, like this:
- message: ‘Logged in: %s’, args: (‘Ritsu Tainaka’,)
- message: ‘Logged in: %s’, args: (‘Mio Akiyama’,)
Yes, you will be able to group these logs. They are same log, just username is different.
OK, now consider this grouping with formatted strings, it will be not work:
- message: ‘Logged in: Ritsu Tainaka’, args: ()
- message: ‘Logged in: Mio Akiyama’, args: ()
They will be handled as different. Of cause, these messages are totally different.
Practical
Sentry, error logging and aggregation platform, it displays logs grouping by these messages.
So, If you use Sentry, you should provide not formatted message to any loggers. Without this, all logs containing some variables will be handled as different. Yes, as thousands of different logs.