Share the Knowledge
RSS icon Home icon
  • How to allow remote connections to your PostgreSQL 9.0 database server on Ubuntu 10.04 LTS

    Posted on September 8th, 2011 webmaster No comments         Print Print

    Change directory to /etc/postgresql/9.0/main and modify the following configs (I know you’re sick of hearing this, but I recommend you back up your original configs before making any changes):

    postgresql.conf

    In “Connections and Authentication” section:

    From

    
    #listen_addresses = 'localhost'
    #password_encryption = on
    

    To

    
    listen_addresses = '*'
    password_encryption = on
    

    pg_hba.conf

    From

    host    all    all    127.0.0.1/32    md5
    host    all    all    ::1/128    md5
    

    To

    host    all    all    0.0.0.0/0   md5
    host    all    all    ::0/0    md5
    

    Restart the ssh daemon: /etc/init.d/ssh restart

  • How to configure the ‘logging’ module using dictionaries in Python 2.6

    Posted on August 21st, 2011 webmaster No comments         Print Print

    The logging.config module was updated in Python 2.7 and included a function called dictConfig() which takes a dictionary as an argument used to configure the logging module.

    I wanted to use this in my new project so I can keep all my configurations/settings in one Python file but we’re not ready to upgrade to Python 2.7 just yet.  The good news is you can just get the dictconfig module on its own and add it to your project.  I actually just took it from the Django 1.3 package and put it in a package within the project I’m working on.

    Here’s a sample configuration/usage for the ‘root’ logger:

    Configuration:

    LOG_SETTINGS = {
        'version': 1,
        'root': {
            'level': 'NOTSET',
            'handlers': ['console', 'file', 'smtp', 'mongodb'],
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'level': 'INFO',
                'formatter': 'detailed',
                'stream': 'ext://sys.stdout',
            },
            'file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'INFO',
                'formatter': 'detailed',
                'filename': 'logs/MyProject.log',
                'mode': 'a',
                'maxBytes': 10485760,
                'backupCount': 5,
            },
            'smtp': {
                'class': 'logging.handlers.SMTPHandler',
                'level': 'ERROR',
                'formatter': 'email',
                'mailhost': 'localhost',
                'fromaddr': 'alerts@calazan.com',
                'toaddrs': ['admin@calazan.com', 'support@calazan.com'],
                'subject': '[My Project] Error encountered.',
            },
            'mongodb': {
                'class': 'log4mongo.handlers.MongoHandler',
                'level': 'DEBUG',
                'host': 'localhost',
                'port': 27017,
                'database_name': 'myproject',
                'collection': 'logs',
                'username': 'logger',
                'password': 'password',
            },
        },
        'formatters': {
            'detailed': {
                'format': '%(asctime)s %(module)-17s line:%(lineno)-4d ' \
                '%(levelname)-8s %(message)s',
            },
            'email': {
                'format': 'Timestamp: %(asctime)s\nModule: %(module)s\n' \
                'Line: %(lineno)d\nMessage: %(message)s',
            },
        },
    }
    

    Usage:

    
    import logging
    import dictconfig
    
    from settings import LOG_SETTINGS
    
    dictconfig.dictConfig(LOG_SETTINGS)
    
    logging.debug('This is a debug message!')
    logging.info('This is an info message!')
    logging.error('This is an error message!')
    
  • How to print the values of all the fields of an object in Java

    Posted on August 17th, 2011 webmaster No comments         Print Print

    I was just doing some Java coding and I needed to check the values of a bunch of fields of an object (mostly numbers).  Normally I’d just call and print each get() method if I just need to check a few fields, but the class I’m dealing with has over 30 fields and I need to check them all and there’s no way I’m doing over 30 print statements for each get() method!

    A quick Google search led me to this StackOverflow post which has exactly what I was looking for:

    ClassABC abc = new ClassABC();
    for (Field field : abc.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        String name = field.getName();
        Object value = field.get(abc);
        System.out.printf("%s: %s%n", name, value);
    }