Logging - Delete files after n days, make a new one, if size greater than 20MByte, code row inc ase of exception
-
Hello,
The qDebug is sufficient for me if there were two options.
Delete files after n days
if size greater than 20MByte,than create additional with the name of current file and prefix "-01.Log"
Log exception code rowDoes anyone have an example?
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { static QFile file("C:\\MyProjectInk\\log\\_Qt\\log_" + QDateTime::currentDateTime().toString("yyyy-MM-dd") + ".Log"); if (!file.isOpen()) { file.open(QFile::WriteOnly | QFile::Append); } QDateTime jetzt = QDateTime::currentDateTime(); QString dateTime = jetzt.toString("yyyy-MM-dd hh:mm:ss.zzz"); QTextStream stream(&file); stream << dateTime << " - " << msg << endl; } int main(int argc, char *argv[]) { qInstallMessageHandler(myMessageOutput); //.. QString help = QString("[CUSTOM][ToInk] initialized= '%1' socket->state()= '%2'") .arg(initialized ? "true" : "false") .arg(socket->state()); qDebug() << help;
-
Hello,
The qDebug is sufficient for me if there were two options.
Delete files after n days
if size greater than 20MByte,than create additional with the name of current file and prefix "-01.Log"
Log exception code rowDoes anyone have an example?
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { static QFile file("C:\\MyProjectInk\\log\\_Qt\\log_" + QDateTime::currentDateTime().toString("yyyy-MM-dd") + ".Log"); if (!file.isOpen()) { file.open(QFile::WriteOnly | QFile::Append); } QDateTime jetzt = QDateTime::currentDateTime(); QString dateTime = jetzt.toString("yyyy-MM-dd hh:mm:ss.zzz"); QTextStream stream(&file); stream << dateTime << " - " << msg << endl; } int main(int argc, char *argv[]) { qInstallMessageHandler(myMessageOutput); //.. QString help = QString("[CUSTOM][ToInk] initialized= '%1' socket->state()= '%2'") .arg(initialized ? "true" : "false") .arg(socket->state()); qDebug() << help;
@RobertSommer said in Logging - Delete files after n days, make a new one, if size greater than 20MByte, code row inc ase of exception:
Does anyone have an example?
Look here: https://github.com/MEONMedical/Log4Qt
-
@RobertSommer said in Logging - Delete files after n days, make a new one, if size greater than 20MByte, code row inc ase of exception:
Does anyone have an example?
Look here: https://github.com/MEONMedical/Log4Qt
@Christian-Ehrlicher
Thanks for the quick reply, isn't there a simple solution? So without a third-party provider. Just deleting the file after 10 (n) days, the line number would be good in case of an exception. -
@Christian-Ehrlicher
Thanks for the quick reply, isn't there a simple solution? So without a third-party provider. Just deleting the file after 10 (n) days, the line number would be good in case of an exception.@RobertSommer
If you don't want to use a third party's code then you have to write it yourself, maybe using some of the code.Delete files after n days
if size greater than 20MByte,than create additional with the name of current file and prefix "-01.Log"
Log exception code row
Qt does not pre-supply dedicated functions to do these, though you can write code with the help of Qt to do so.
qDebug()
certainly does not do such things. -
To delete the old log files, you need
- first to get the list of files in the appropriate directory (QDir::entryList)
- and filter them by date
So as QDir::entryList offers the possibility to pass filter arguments, this should be possible to do those two steps at once.In fact no, those filters don't allow for file size filtering. You have to loop over the list and remove those too new (reminder, don't remove list elements in a while or for loop, you skip the element next to the one that has been removed !!!) Yours to see if you perform it only at app startup (if your app has a relatively short runtime) or if you also need to set a timer to perform the process at regular interval (which should be needed in case of a server for example).To create a new file when the current one is too big, you would have to monitor the size of the current output file, and once the threshold has been reached, close it and create a new file with a new file name.
I would create a dedicated class which all logs pass through and holds the responsability of creating and closing the files. After each write in it, check the file size, and once the threshold is reached, close it and recreate a new one. All this transparently to the rest of the application.and as @JonB said, qDebug() is not what you need to write to a file.
-
@RobertSommer
If you want a model to copy, Python's RotatingFileHandler and more specifically TimedRotatingFileHandler seem to provide what you are speaking about. As you can see it requires a library and a bunch of code to implement, you could write similar using C++ and some Qt calls.