#!/usr/bin/env python
###Import Library###
import logging, os, time
import logging.handlers
from logging.handlers import RotatingFileHandler

###script Name: Ops401challenge28###
###Author: Bubacarr Darboe###
###Date: 11/11/2020###
###Description of purpose: logging errors3###

###Declaration of variable###
logger = logging.getLogger(__name__)


###Declaration of Functions###
###Main###

print("let's create handlers...")
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
c_handler.setLevel(logging.WARNING)
f_handler.setLevel(logging.ERROR)
time.sleep(3)

###Add handlers to the logger###
logger.addHandler(c_handler)
logger.addHandler(f_handler)
time.sleep(3)


print("Generating one warning log and one error log...")
logger.warning('Warning generated')
logger.error('Error generated')
time.sleep(3)

print("Program complete. View 'file.log' to see errors.")
os.system("ls -al")


handler = logging.handlers.WatchedFileHandler(
    os.environ.get("LOGFILE", "/var/log/yourapp.log"))
formatter = logging.Formatter(logging.BASIC_FORMAT)
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(os.environ.get("LOGLEVEL", "INFO"))
root.addHandler(handler)
 
try:
    exit(main())
except Exception:
    logging.exception("Exception in main()")
    exit(1)

logger = logging.getLogger('my_logger')
handler = RotatingFileHandler('my_log.log', maxBytes=200, backupCount=10)
logger.addHandler(handler)

for i in range(1000): #set each VALUE
    logmsg = "Hello world!"
    logmsg += str(i)
    logger.warning(logmsg)
    print ("Logging Hello world! number", i)
    os.system("ls -al")
    time.sleep(1)



###End###
