Skip to content

Instantly share code, notes, and snippets.

@akanik
Created March 18, 2016 03:28
Show Gist options
  • Save akanik/fcf9a51483d4d3f709ab to your computer and use it in GitHub Desktop.
Save akanik/fcf9a51483d4d3f709ab to your computer and use it in GitHub Desktop.
logging text and Exception
from sys import argv
import datetime, os, logging
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
def handle(self, *args, **options):
self.print_date()
def print_date(self):
file_base_path = os.path.dirname(os.path.realpath(__file__))
date = '{:%Y-%m-%d_%H_%M_%S}'.format(datetime.datetime.now())
file_extention = '.txt'
file_path = file_base_path + '/' + date + file_extention
log_file = open(file_path, 'w')
log_file.write("Here's some log text that we're going to print to the file.")
log_file.write("\nThe date is " + date + '\n')
def main():
raise Exception('Hey!')
logging.basicConfig(level=logging.DEBUG, filename=file_path)
try:
main()
except:
logging.exception('Oops:')
log_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment