Created
March 18, 2016 03:28
-
-
Save akanik/fcf9a51483d4d3f709ab to your computer and use it in GitHub Desktop.
logging text and Exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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