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

Keep same file name for log and rotate the file with timestamp #302

Open
singaselvasankar opened this issue Feb 1, 2021 · 12 comments
Open

Comments

@singaselvasankar
Copy link

singaselvasankar commented Feb 1, 2021

logger.txt

I'm using [email protected] and [email protected]
In my logger configuration I have the following.

var transportError = new (winston.transports.DailyRotateFile)({
  filename: HOME_DIR + '/logs/backend/error/error-%DATE%.log',
  datePattern: 'YYYY-MM-DD-HH',
  zippedArchive: false,
  maxSize: '50m',
  maxFiles: '14d',
  level: 'error',
  name: 'error',

  createSymlink: true,
  symlinkName: 'error.log',
  tailable: true,

  handleExceptions: true,
  json: true,
  colorize: false,
});

this is supposed to be like current log file should be error.log and when rotates it should get append with date format

even though I have mentioned createSymlink, tailable set to true symlinkName with 'error.log'
my current log files are like this error-2021-02-01-13.log, error-2021-02-01-12.log

@mattberther
Copy link
Member

This is correct - and you should see a symlink in the /logs/backend/error folder called error.log, which is a symlink to the current log file. Please reopen with additional detail if you find this to not be the case.

@singaselvasankar
Copy link
Author

Hi @mattberther thanks, but I am not seeing error.log in /logs/backend/error. Could you tell me how do I get additional details for you in this case. I'm using windows machine.

@mattberther
Copy link
Member

Below is a shell session showing that this should work. Can you try this simple program on your system and let us know what happens?

~/projects/github.com/winston-daily-rotate-file master
❯ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

~/projects/github.com/winston-daily-rotate-file master
❯ cat test.js
var winston = require('winston');
var randomString = require('./test/random-string');
require('./index');

const transport = new (winston.transports.DailyRotateFile)({
    level: 'info',
    filename: 'logs/application-%DATE%.log',
    datePattern: 'YYYY-MM-DD',
    zippedArchive: false,
    maxSize: '1k',
    maxFiles: '14d',
    level: 'error',
    name: 'error',

    createSymlink: true,
    symlinkName: 'error.log',

    handleExceptions: true,
    json: true,
    colorize: false,
});


const logger = winston.createLogger({
    level: 'info',
    format: winston.format.combine(
        winston.format.timestamp({
            format: 'DD-MM-YYYY-hh-MM',
        }),
        winston.format.printf((info) => {
            return `${info.timestamp} ${info.level} ${info.message}`;
        })),
    transports: [
        transport,
    ],
});

logger.error(randomString(2048))
logger.error("This is expected in the info transport")

~/projects/github.com/winston-daily-rotate-file master
❯ rm -rf logs && node test.js && ls -al logs
total 24
drwxr-xr-x   6 matt  staff   192 Feb  2 19:36 .
drwxr-xr-x  20 matt  staff   640 Feb  2 19:36 ..
-rw-r--r--   1 matt  staff   498 Feb  2 19:36 .a28f20db971a7c5f03cc3cf0d21bff08893fc8f3-audit.json
-rw-r--r--   1 matt  staff  2072 Feb  2 19:36 application-2021-02-02.log
-rw-r--r--   1 matt  staff    62 Feb  2 19:36 application-2021-02-02.log.1
lrwxr-xr-x   1 matt  staff    28 Feb  2 19:36 error.log -> application-2021-02-02.log.1

~/projects/github.com/winston-daily-rotate-file master
❯

@singaselvasankar
Copy link
Author

singaselvasankar commented Feb 3, 2021

@mattberther as you suggested ran the simple program, got the following in logs. I don't see error.log
Used the simple program you posted above
in both the log files I see the message one in random string and another one the message provided in the js

 Directory of D:\winston-daily-rotate-file\logs

02/03/2021  02:12 PM    <DIR>          .
02/03/2021  02:12 PM    <DIR>          ..
02/03/2021  02:12 PM               501 .a28f20db971a7c5f03cc3cf0d21bff08893fc8f3-audit.json
02/03/2021  02:12 PM             2,073 application-2021-02-03.log
02/03/2021  02:12 PM                63 application-2021-02-03.log.1
               3 File(s)          2,637 bytes
               2 Dir(s)  14,822,420,480 bytes free

@mattberther
Copy link
Member

From your project's root folder, what is the output of npm view file-stream-rotator version?

Further, it seems as though creating symlinks on Windows requires admin permissions (see nodejs/node#18518 (comment)). Let me know if that solves the issue, and if so, I'll submit a PR against the file-stream-rotator component to add the required option to bypass this.

@mattberther mattberther reopened this Feb 3, 2021
@singaselvasankar
Copy link
Author

Checked in project's root folder file-stream-rotator version is 0.5.7
Ran the sample application with admin privilege, still no file as error.log

@jalbiero
Copy link

I don't understand the symlink options. Why not have the current file without any date/number and just apply them after a rotation? That's what I need and what I had used in other languages. IMHO the mentioned options over-complicate the usage.

@hezhongfeng
Copy link

same what I want.

@Darshilp326
Copy link

@singaselvasankar Where you able to solve this issue?

@singaselvasankar
Copy link
Author

@singaselvasankar Where you able to solve this issue?

No @Darshilp326 I couldn't, but symlink is working as expected in my Linux system

@mohsenmahoski
Copy link

I don't understand the symlink options. Why not have the current file without any date/number and just apply them after a rotation? That's what I need and what I had used in other languages. IMHO the mentioned options over-complicate the usage.

I don't want to config my elastic search to watch the file with pattern. I thing its very important to have the current file without any pattern

@DaCao
Copy link

DaCao commented Dec 1, 2022

This answer here worked very well for me: #310 (comment)

I think you are missing the dirname: './logs', line. Below is what worked in my app:

const transportDailyRotate: DailyRotateFile = new DailyRotateFile({
  filename: 'logs/application-%DATE%.log',
  datePattern: 'YYYY-MM-DD',
  maxSize: '20m',
  maxFiles: '30d',
  dirname: './logs',
  createSymlink: true,
  symlinkName: 'all.log',
  format: format_string,
});

const transports = [
  new winston.transports.Console(options.conosle),
  new winston.transports.File(options.error_logfile),
  transportDailyRotate,
];

const Logger = winston.createLogger({
  level: level(),
  levels,
  defaultMeta: { service: 'myapp', host: os.hostname() },
  transports,
});

and in the ./logs folder:

-rw-rw-r-- 1 ubuntu ubuntu 5047 Dec  2 01:59 application-2022-12-02.log
drwxrwxr-x 2 ubuntu ubuntu 4096 Dec  2 01:52 .
lrwxrwxrwx 1 ubuntu ubuntu   26 Dec  2 01:52 all.log -> application-2022-12-02.log
-rw-rw-r-- 1 ubuntu ubuntu    0 Dec  2 01:52 error.log

I think this issue can be closed.

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

No branches or pull requests

7 participants