import os
import sys

runPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(runPath, ".."))

from apps.cache import Cache
cache = Cache()
cache.clear_log_collection()

from settings import settings
st = settings.Settings()
cfg = st.load()

use_log_server = cfg['log_use_server']
use_log_file = cfg['log_use_file']
use_log_redis = cfg['log_use_redis']

filename = cfg['log_filename'] + '.txt'

if use_log_file:
    with open(filename, 'w') as f:
        f.write('')

import logging
logging.basicConfig(
    format='%(asctime)s %(message)s',
    level=logging.INFO
)

def makelog(source, message, code):
    text = 'From [%s] : [%s] with code [%s]' % (source, message, code)
    logging.info(text)

    if use_log_server:
        pass

    if use_log_redis:
        cache.push_item(
            text,
            cache.collection_log
        )

    if use_log_file:
        with open(filename, 'a') as f:
            f.write(text + '\n')