#! coding:utf-8
import logging
import sys

from .versions import PY3

if PY3:
    unicode = str


def init_logger(
    name="",
    handler_path_levels=None,
    level=logging.INFO,
    formatter=None,
    formatter_str=None,
    datefmt="%Y-%m-%d %H:%M:%S",
):
    """Add a default handler for logger.
    
    Args:

    name = '' or logger obj.

    handler_path_levels = [['loggerfile.log',13],['','DEBUG'],['','info'],['','notSet']] # [[path,level]]

    level = the least level for the logger.

    formatter = logging.Formatter(
            '%(levelname)-7s %(asctime)s %(name)s (%(filename)s: %(lineno)s): %(message)s',
             "%Y-%m-%d %H:%M:%S")

    formatter_str = '%(levelname)-7s %(asctime)s  %(name)s (%(funcName)s: %(lineno)s): %(message)s'

    custom formatter:
        %(asctime)s  %(created)f  %(filename)s  %(funcName)s  %(levelname)s  %(levelno)s  %(lineno)s   %(message)s   %(module)s    %(name)s   %(pathname)s   %(process)s   %(relativeCreated)s   %(thread)s  %(threadName)s  
    """
    levels = {
        "NOTSET": logging.NOTSET,
        "DEBUG": logging.DEBUG,
        "INFO": logging.INFO,
        "WARNING": logging.WARNING,
        "ERROR": logging.ERROR,
        "CRITICAL": logging.CRITICAL,
    }
    if not formatter:
        if formatter_str:
            formatter_str = formatter_str
        else:
            formatter_str = "%(levelname)-7s %(asctime)s %(name)s (%(filename)s: %(lineno)s): %(message)s"
        formatter = logging.Formatter(formatter_str, datefmt=datefmt)
    logger = name if isinstance(name, logging.Logger) else logging.getLogger(str(name))
    logger.setLevel(level)
    handler_path_levels = handler_path_levels or [["", "INFO"]]
    # ---------------------------------------
    for each_handler in handler_path_levels:
        path, handler_level = each_handler
        handler = logging.FileHandler(path) if path else logging.StreamHandler()
        handler.setLevel(
            levels.get(handler_level.upper(), 1)
            if isinstance(handler_level, str)
            else handler_level
        )
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    return logger


dummy_logger = init_logger(
    "torequests.dummy",
    formatter_str="%(levelname)-7s %(asctime)s %(name)s: %(message)s",
)
main_logger = init_logger(
    "torequests.main", formatter_str="%(levelname)-7s %(asctime)s %(name)s: %(message)s"
)
utils_logger = init_logger(
    "torequests.utils",
    formatter_str="%(levelname)-7s %(asctime)s %(name)s: %(message)s",
)
print_logger = init_logger(
    "torequests.print", formatter_str="[%(asctime)s](L%(line_no)s): %(message)s"
)


def print_info(*messages, **kwargs):
    """kwargs: sep=' '"""
    sep = kwargs.pop("sep", " ")
    line_no = sys._getframe(1).f_lineno
    return print_logger.info(
        sep.join(map(unicode, messages)), extra={"line_no": line_no}
    )

@@ -24,16 +24,26 @@
 logging%0A
+import os%0A
 import s
@@ -2626,19 +2626,20 @@
 e)s%5D
-(L%25(line_no
+ %25(fn)s(%25(ln
 )s):
@@ -2704,23 +2704,394 @@
  %22%22%22
-kwargs: sep=' '
+Simple print use logger, print with time / file / line_no.%0A        :param sep: sep of messages, %22 %22 by default.%0A%0A    Basic Usage::%0A%0A        print_info(1, 2, 3)%0A        print_info(1, 2, 3)%0A        print_info(1, 2, 3)%0A%0A        # %5B2018-10-24 19:12:16%5D temp_code.py(7): 1 2 3%0A        # %5B2018-10-24 19:12:16%5D temp_code.py(8): 1 2 3%0A        # %5B2018-10-24 19:12:16%5D temp_code.py(9): 1 2 3%0A    
 %22%22%22%0A
@@ -3127,23 +3127,21 @@
  %22)%0A    
-line_no
+frame
  = sys._
@@ -3155,17 +3155,114 @@
 e(1)
-.f_lineno
+%0A    ln = frame.f_lineno%0A    _file = frame.f_globals.get(%22__file__%22, %22%22)%0A    fn = os.path.split(_file)%5B-1%5D
 %0A   
@@ -3343,24 +3343,24 @@
 =%7B%22l
-ine_no%22: line_no
+n%22: ln, %22fn%22: fn
 %7D%0A  

add filename for print_info