logging

http://inventwithpython.com/blog/2012/04/06/stop-using-print-for-debugging-a-5-minute-quickstart-guide-to-pythons-logging-module/

log.py

#!/usr/bin/python

import logging

logging.basicConfig(level=logging.CRITICAL, format='%(asctime)s - %(levelname)s - %(message)s')

logging.debug('This is a log message.')

logging.critical('This is a Critical log message.')

output:

python log.py

2015-09-29 20:43:15,057 - CRITICAL - This is a Critical log message.

log2.py

#!/usr/bin/python

import sys, getopt, logging

log_level = logging.CRITICAL

options, remainder = getopt.getopt(sys.argv[1:], 'v', ['verbose'])

cmdargs = {}

for opt, arg in options:

    if opt in ('-v', '--verbose'):

        log_level = logging.DEBUG

logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s - %(message)s')

logging.debug('This is a debug log message.')

logging.critical('This is a Critical log message.')

output:

python log2.py

2015-09-30 11:20:35,084 - CRITICAL - This is a Critical log message.

python log.py -v

2015-09-30 11:20:37,235 - DEBUG - This is a debug log message.

2015-09-30 11:20:37,236 - CRITICAL - This is a Critical log message.

logging with requests module

log.py

#!/usr/bin/python

import getpass, requests, json

import sys, getopt, logging

log_level = logging.CRITICAL

#log_level = logging.DEBUG

options, remainder = getopt.getopt(sys.argv[1:], 'v', ['verbose'])

cmdargs = {}

for opt, arg in options:

if opt in ('-v', '--verbose'):

log_level = logging.DEBUG

print "Current log level is: {}".format(logging.getLevelName(log_level))

logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s - %(message)s')

#logging.debug('This is a debug log message.')

#logging.critical('This is a Critical log message.')

requests.packages.urllib3.disable_warnings()

BIGIP_USER = 'admin'

BIGIP_PASS=getpass.getpass("Please enter your password: ")

ACTIVE_NODE = 'f5.blah.com'

BIGIP_URL_BASE = 'https://{}/mgmt/tm'.format(ACTIVE_NODE)

bigip = requests.session()

bigip.auth = (BIGIP_USER, BIGIP_PASS)

bigip.verify = False

bigip.headers.update({'Content-Type' : 'application/json'})

def check_failoverstatus(SESSION, BIGIP_URL_BASE):

r = SESSION.get('{}/cm/failover-status'.format(BIGIP_URL_BASE))

if r.status_code == 200:

True

#the next line is not needed as it seems the requests module already logs DEBUG data how great is that :)

#logging.debug('Method:{} URL:{} Status code: {}'.format(r.request.method,r.request.url,r.status_code))

else:

logging.critical('Method:{} URL:{} Status code: {}'.format(r.request.method,r.request.url,r.status_code))

data = r.json()

device = {}

device['FailoverStatus'] = data['entries'].values()[0]['nestedStats']['entries']['status']['description']

device['version'] = data['selfLink'].split('?')[1].split('=')[1]

return device

def check_syncstatus(SESSION, BIGIP_URL_BASE):

r = SESSION.get('{}/cm/sync-status'.format(BIGIP_URL_BASE))

data = r.json()

LB_Sync_Status = data['entries'].values()[0]['nestedStats']['entries']['status']['description']

return LB_Sync_Status

BIGIP_FAILOVER_STATUS = check_failoverstatus(bigip, BIGIP_URL_BASE)

BIGIP_SYNC_STATUS = check_syncstatus(bigip, BIGIP_URL_BASE)