GaiZhenbiao / ChuanhuChatGPT

GUI for ChatGPT API and many LLMs. Supports agents, file-based QA, GPT finetuning and query with web search. All with a neat UI.
https://huggingface.co/spaces/JohnSmith9982/ChuanhuChatGPT
GNU General Public License v3.0
15.24k stars 2.3k forks source link

[功能请求]: 将log保存到文件,group by day, 且文件名为当天日期,代码已由GPT-4 写好 #421

Open LanceLiMS opened 1 year ago

LanceLiMS commented 1 year ago

相关问题

如题,可否将log保存到文件,且文件名为当天日期,代码我已让Azure GPT-4 API 写好

可能的解决办法

system:

user:

assistant:

Here's a step-by-step solution:

  1. Import required libraries:

    import os
    import logging
    from logging.handlers import TimedRotatingFileHandler
    from datetime import datetime
  2. Create a directory named 'logs' if it doesn't exist:

    log_dir = "logs"
    if not os.path.exists(log_dir):
    os.makedirs(log_dir)
  3. Set up a function to configure the logging mechanism:

def setup_logging():
    # Create a custom logger
    logger = logging.getLogger("my_logger")
    logger.setLevel(logging.DEBUG)

    # Create a log file with the current date
    current_date = datetime.now().strftime('%Y-%m-%d')
    log_file = os.path.join(log_dir, f'{current_date}.log')

    # Create a file handler that rotates the log file daily
    file_handler = TimedRotatingFileHandler(log_file, when = "midnight", interval = 1, encoding='utf-8'))
    file_handler.suffix = "%Y-%m-%d.log"
    file_handler.setLevel(logging.DEBUG)

    # Create a console handler
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)

    # Create a formatter
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)

    # Add the file handler and console handler to the logger
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)

    return logger
  1. Now use the logger to log messages:
logger = setup_logging()

logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")

When you run the Python script, it will create log files with the current date as their names in the 'logs' directory. The log files will also display on the console.

You can also configure the logging format, level, and rotation settings as needed.

user:

assistant:

Follow these steps:

  1. Create a new Python file called logger_config.py in your project's root directory.

  2. In logger_config.py, place the code for the logging configuration:

#logger_config.py

import os
import logging
from logging.handlers import TimedRotatingFileHandler
from datetime import datetime

log_dir = "logs"
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

def setup_logging():
    logger = logging.getLogger("my_logger")
    logger.setLevel(logging.DEBUG)

    current_date = datetime.now().strftime('%Y-%m-%d')
    log_file = os.path.join(log_dir, f'{current_date}.log')

    file_handler = TimedRotatingFileHandler(log_file, when="midnight", interval=1, encoding='utf-8'))
    file_handler.suffix = "%Y-%m-%d.log"
    file_handler.setLevel(logging.DEBUG)

    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)

    logger.addHandler(file_handler)
    logger.addHandler(console_handler)

    return logger
  1. In each Python file where you need to use the logger, import the setup_logging() function from logger_config.py and create a logger instance:
# main.py (or any_other_file.py)

from logger_config import setup_logging

logger = setup_logging()
logger.debug("This is a debug message from main.py")

This way, you can use the same logging configuration across all your Python files by just importing and calling the setup_logging() function.

If you want to customize the logger name for each file, you can modify the setup_logging function to accept a name parameter:

def setup_logging(logger_name="my_logger"):
    ...
    logger = logging.getLogger(logger_name)
    ...

And then, when calling it in other modules, you can pass a custom name:

logger = setup_logging(logger_name="main")

帮助开发

补充说明

No response

kbqncf commented 1 year ago

非常可以,GPT-4。想问下老哥,Azure要使用GPT的service是不是必须得用海外公司主体来申请的

LanceLiMS commented 1 year ago

非常可以,GPT-4。想问下老哥,Azure要使用GPT的service是不是必须得用海外公司主体来申请的

现在的申请流程是:

  1. 用公司邮箱,以公司名义申请azure openai 的access权限,现在是脚本审批,很快就能通过,个人账户都会被拒绝 https://aka.ms/oai/1p/aoaionboarding
  2. access通过后只能用到gpt-35-turbo, gpt4要用上一步通过的application id再额外申请加waitlist apply by filling out this form