Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to adjust the time string which is inserted when rotating? #899

Closed
changchiyou opened this issue Jun 25, 2023 · 8 comments
Closed

How to adjust the time string which is inserted when rotating? #899

changchiyou opened this issue Jun 25, 2023 · 8 comments
Labels
question Further information is requested

Comments

@changchiyou
Copy link

The code below generate record.2023-06-25_21-37-48_944911.log after rotating The time string 2023-06-25_21-37-48_944911 is the time when the script has been launched.

from logger import logger

logger.remove()
logger.logger.add("record.log", rotation="00:00")

# do something

Is it possible to adjust the format of time string?

To be more specific, I want record.2023-06-25.log(record.{yesterday date time: YYYY-MM-DD}.log) would be generate on 2023-06-26 00:00, instead of record.2023-06-25_21-37-48_944911.log(record.{script launching time}.log).


I have read Logger.add but still have no idea about my question.

@Delgan
Copy link
Owner

Delgan commented Jun 25, 2023

Hi.

You can define a custom compression function to actually rename the file as desired:

def rename(filepath):
    today = datetime.datetime.now().strftime("%Y-%m-%d")
    os.rename(filepath, f"record.{today}.log")

logger.add("record.log", rotation="00:00", compression=rename)

@Delgan Delgan added the question Further information is requested label Jun 25, 2023
@changchiyou
Copy link
Author

changchiyou commented Jun 26, 2023

@Delgan Thanks for your replay 👍

In order to avoid misunderstanding, I would like to ask detailed questions about your answer: Is the codes in your answer just a example about how to use compression parameter or that is the exact solution for my needs?


From my understanding in the codes you provied to me, rename convert the record.log to record.2023-06-26.log on 2023-06-26 00:00. But I thought record.2023-06-26.log should means "the record of logs happened in 2023-06-26".

def rename(filepath):
    today = datetime.datetime.now().strftime("%Y-%m-%d")
    os.rename(filepath, f"record.{today}.log")

logger.add("record.log", rotation="00:00", compression=rename)

Should I change the example you gave me to

def rename(filepath):
    today = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    os.rename(filepath, f"record.{today}.log")

logger.add("record.log", rotation="00:00", compression=rename)

or

def rename(filepath):
    today = datetime.datetime.now().strftime("%Y-%m-%d")
    os.rename(filepath, f"record.{today}.log")

logger.add("record.log", rotation="24:00", compression=rename)

for my own needs?

@Delgan
Copy link
Owner

Delgan commented Jun 26, 2023

Sorry, that was an oversight. I think you indeed need to remove datetime.timedelta(days=1) to get the desired result. 👍

@changchiyou
Copy link
Author

Sorry, that was an oversight. I think you indeed need to remove datetime.timedelta(days=1) to get the desired result. 👍

You are right, the answer above you provide to me is correct.


and the code above(in my question) is incorrect:

  • - datetime.timedelta(days=1) is needed to remove:

Should I change the example you gave me to

def rename(filepath):
    today = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    os.rename(filepath, f"record.{today}.log")

logger.add("record.log", rotation="00:00", compression=rename)
  • rotation="24:00" would get a format error exception:

or

def rename(filepath):
    today = datetime.datetime.now().strftime("%Y-%m-%d")
    os.rename(filepath, f"record.{today}.log")

logger.add("record.log", rotation="24:00", compression=rename)

for my own needs?

@Delgan
Copy link
Owner

Delgan commented Jun 27, 2023

Be careful there might be a bug with rotation though: #894

@changchiyou
Copy link
Author

changchiyou commented Jul 6, 2023

It's not a bug report of loguru, just wanna share my mis-understanding of the answer above


I got another error and couldn't find out for days:

The answer code provided by @Delgan works perfectly

Hi.

You can define a custom compression function to actually rename the file as desired:

def rename(filepath):
    today = datetime.datetime.now().strftime("%Y-%m-%d")
    os.rename(filepath, f"record.{today}.log")

logger.add("record.log", rotation="00:00", compression=rename)

But I have not noticed that the input argument filepath is already renamed when rotating, instead of keeping origin file name. For example, If I set loguru.logger via logger.add("record.log", ...) the filepath would be like

/[abs_paht]/record.2023-07-06_08-56-33_328410.log

instead of (what I thought before)

/[abs_paht]/record..log

Therefore I got a serious bug when I tried to rename from filepath. 😢

@changchiyou
Copy link
Author

@Delgan I found that the build-in solution in #894 :

from loguru import logger

logger.remove()
logger.add(
    'logs/log.{time:YYYY-MM-DD}.log',
    level='INFO',
    rotation="00:00",
    retention='1 months',
    enqueue=True
)

logger.info('hello world')
logger.info('hello world')

Which would you suggest, the answer above #899 (comment) or this build-in solution?

@changchiyou changchiyou reopened this Jul 9, 2023
@Delgan
Copy link
Owner

Delgan commented Jul 9, 2023

@changchiyou It depends on your preferences.

Using the built-in solution the current logged file will contain the date, e.g. "record.2023-07-09.log", whereas with your solution, the current file will simply be "record.log".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants