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

App doesn't write to Logstash except one logger #185

Open
efratha94 opened this issue Apr 21, 2021 · 0 comments
Open

App doesn't write to Logstash except one logger #185

efratha94 opened this issue Apr 21, 2021 · 0 comments

Comments

@efratha94
Copy link

Hi,
I tried connecting my app to Logstash & ES with winston and winston-elasticsearch, but for some reason, there's only one logger.log function that writes to Logstash, from main.js, while all other logger.log don't get logged (from main.js and from other modules).

This is my logger.js module:

const { createLogger, transports, format, level } = require("winston")
const { ElasticsearchTransport } = require('winston-elasticsearch');
const path =                       require("path");

const elasticsearchTransportOpts = new ElasticsearchTransport({ 
    level: "debug",
    useTransformer: false,
    clientOpts: {
        node: "http://logstash-url:1234"
    }
})

const logFormat = format.printf(info => `${info.timestamp} ${info.level} [${info.label}]: ${info.message}`)

const logConfiguration = {
    level: "debug",
    format: format.combine(
        format.timestamp({ format: 'DD-MM-YYYY HH:mm:ss' }),
        format.splat()
    ),
    transports: [
        new transports.Console({
            level: 'silly',
            format: format.combine(format.colorize(), logFormat)
        }),
        new transports.File({
            level: 'info',
            filename: "logs/info.log",
            format: format.combine(format.json())
        }),
        new transports.File({
            level: 'debug',
            filename: "logs/debug.log",
            format: format.combine(format.json())
        }),
        new transports.File({
            level: 'warn',
            filename: "logs/warn.log",
            format: format.combine(format.json())
        }),
        new transports.File({
            level: 'error',
            filename: "logs/error.log",
            format: format.combine(format.json())
        }),
        elasticsearchTransportOpts
    ],
    defaultMeta: { type: "custom_field", "extra.environment": "development" }
};

const logger = createLogger(logConfiguration)

This is my main.js, which has several logger.log functions:

const logger =              require("./config/logger")   

async function treatFiles() {
    try {
        let zipFilesPaths = await findZipFiles()
        let zipExtract = await extractZipFiles(zipFilesPaths)

        if (zipExtract.length > 0) {

            zipExtract.map(async elem => {
                let fileParsing = await parseXML(elem)
                let zipFileRenaming = await convertToPDX(fileParsing)
                await sendFTP(zipFileRenaming)
                return elem

            })

            return
        
        } else {
            logger.log("info", `No files to transfer`, { label: path.basename(__filename)})
        }
        return 

    } catch (err) {

        ... error handling
    }
    
}

cron.schedule('* * * * *', () => {
    logger.log("debug", `Running a task every minute`, { label: path.basename(__filename)})
    treatFiles()
}, { start: true })

The logger at the bottom, 'Running a task every minute' gets logged to Logstash and ES as expected, but the logger for 'No files to transfer' doesn't get logged at all, and logger.log functions from other modules in the project don't get logged too.
All the logs are written into a file as well successfully, but not into Logstash.

Can you please help? I've been trying to work on it for days now.
Thanks!

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

1 participant