winston and loggly nothing shows up on loggly dashboard - node.js

Trying to set up loggly with winston, and nothing shows up! I tried a catch-all source group:
And tried a simple info log:
winston = require 'winston'
Loggly = require('winston-loggly').Loggly
winston.add Loggly, {
subdomain: "my-subdomain",
inputToken: "my-input-token-ihawof9ahw3fo9ahwe",
json: true
}
winston.info 'Hello Loggly!'
What could be wrong?

Loggly released new version - Gen2. Gen2 is not implemented in winston-loggly package yet. After my communication with Loggly Team I found out a solution based on this issue comment:
var winston = require('winston');
require('winston-loggly');
var logger = new (winston.Logger)({
transports: [
//new (winston.transports.Console)(),
new (winston.transports.Loggly)({
inputToken: 'mytoken',
subdomain: 'mydomain',
auth: { username: 'myusername', password: 'pswd' },
json: true
})
]
});
Object.defineProperty(logger.transports.loggly.client.config, 'inputUrl', {
value: 'https://logs-01.loggly.com/inputs/',
enumerable: true,
configurable: true
});
logger.info('Hello Loggly!');

Related

How to use fluentbit with Nestjs?

I have a fluentbit running in the Docker container 192.168.1.201:24224 which is connected to the elasticsearch.
Now, I am trying to connect my nestjs logger to the fluentbit:
logger.ts
import * as winston from 'winston';
var config = {
host: '192.168.1.201',
port: 24224,
timeout: 3.0,
requireAckResponse: true, // Add this option to wait response from Fluentd certainly
};
var fluentTransport = require('fluent-logger').support.winstonTransport();
var fluent = new fluentTransport('test', config);
// Initialize Winston Logger
const logger = winston.createLogger({
level: 'verbose',
format: winston.format.json(),
defaultMeta: { service: 'test-service' },
transports: [
fluent,
new winston.transports.Console({
format: winston.format.json(),
}),
],
});
export default logger;
I am using the logger in this way:
loggservice.ts:
async log_info(
tracking_id: any,
) {
logger.log({
tracking_id: tracking_id,
});
}
then, in the function :
test.ts:
log_info(tracking_id)
It seems that it can not send the log to the fluentbit(so that I can get the log in ES), am I missing something here?

How to add multiple transport for parse-server logger?

I can access to the winston logger exposer by parse-server with let logger = require('parse-server').logger;
I would like to configure several transport file to have separate file with different logging levels like I can do with Winston:
var logger = new (winston.Logger)({
transports: [
new (winston.transports.File)({
name: 'info-file',
filename: 'filelog-info.log',
level: 'info'
}),
new (winston.transports.File)({
name: 'silly-file',
filename: 'filelog-silly.log',
level: 'silly'
})
]
});
I've tried by usgin winston function like:
logger.add(winston.transports.File, { ... });
And with:
winston.configure({
transports: [
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
But it doesn't work.
I also saw this PR https://github.com/parse-community/parse-server/pull/2363 but I coudn't understand how to add these transports from my index.js in parse-server.
Can someone give me some hints? Thanks!
Additional info:
parse-server version: 2.6.2
I was trying to do similar as you. I wanted to add a transport to send logs to loggly.
I think the way to do it, is to use the logger object, that is exported together with the ParseServer object from the parse-server package.
The way how I added the loggly additional transport is the following:
const {ParseServer, logger} = require('parse-server');
const loggly = require('winston-loggly');
const parseApi = new ParseServer({
...
});
logger.adapter.addTransport(new loggly.Loggly({
subdomain: 'my-subdomain',
token: 'myToken',
json: true
}));
I believe this is safe to do, as the logger is exported by the package in the same way as ParseServer.
Hope it helps. Lukas
for future reference (an example how I use winston-mongoDB), I'll just add my way as well:
1) create custom logger adapter
var _WinstonLoggerAdapter = require('parse-server/lib/Adapters/Logger/WinstonLoggerAdapter');
import * as WinstonMongoDb from 'winston-mongodb';
export function createCustomLoggerAdapter(options) {
var winston = new _WinstonLoggerAdapter.WinstonLoggerAdapter(options);
var WinstonMongoDbInstance: any = WinstonMongoDb.MongoDB;
const transport = new WinstonMongoDbInstance({
...options,
collection: 'ServerLog',
level: 'error'
});
winston.addTransport(transport);
return winston;
}
2) use it in parse config
var parseConfig = {
....,
loggerAdapter: createCustomLoggerAdapter({db: databaseUri}),
}

Logging only the message using winston

I am using winston logger in my node app and i want to log my custom message to the log file.
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: '/tmp/test.log',
handleExceptions: true,
json: true,
maxsize: 20971520 //20MB
})
],
exitOnError: false
});
I only need to log message in the log, i dont need to log level and timestamp in the log file. Current logged sample is as below.
{"level":"info","message":"sample entry to log","timestamp":"2015-12-11T09:43:50.507Z"}
My intention is to get entry in the log file as below
sample entry to log
How to achieve this?
Winston 3:
import winston from 'winston';
const logger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.Console({
format: winston.format.printf(log => log.message),
})
]
});
logger.info('eat my shorts');
On Winston 2.x
If you want to print only the message, you can achieve this by setting:
json: false,
timestamp: false,
showLevel: false
Full Logger example:
let logger = new Logger({
transports: [
new (transports.File)({
name: 'info-file',
filename: 'filelog-info.log',
json: false,
timestamp: false,
showLevel: false
})
],
exitOnError: false
});
logger.log('info', 'eat my shorts');
gives in filelog-info.log: eat my shorts
FYI: filters and rewriters didn't help here
If anyone, as did I, finds this question in hopes of making express-winston log only message (to the console in my case)
Here is what I did:
const winston = require('winston');
const expressWinston = require('express-winston');
const { MESSAGE } = require('triple-beam');
expressWinston.logger({
transports: [
new winston.transports.Console({
format: simpleformatter()
})
]
});
const simpleformatter = winston.format(info => {
info[MESSAGE] = info.message;
return info;
});
Yep got the one i was looking for
https://github.com/winstonjs/winston#filters-and-rewriters
https://github.com/winstonjs/winston#custom-log-format

Sailsjs - Custom Logging with Winston

I am currently trying to write a custom logger for sailsjs that will use winston to send files to either an s3 bucket or a mongodb database.
The documentation seems to be lacking but so far i have found this:
var customLogger = new winston.Logger({
transports: [
new(winston.transports.File)({
level: 'debug',
filename: './logs/my_log_file.log'
})
]
});
module.exports.log = {
colors: false, // To get clean logs without prefixes or color codings
custom: customLogger
};
Which overall is not working for me.
Any ideas?
After extending above MayBeColin's work, the working solution:
Create a new js file inside a config folder(code inside of this will be executed automatically by sails) and add mongodb transports as below,
var winston = require('winston');
var MongoDB = require('winston-mongodb').MongoDB;
var customLogger = new(winston.Logger)({
transports: [
new(winston.transports.MongoDB)({
db: 'mongodb://localhost:27017/test',
collection: 'logs',
level: 'debug'
})
]
});
module.exports.logging = {
colors: false, // To get clean logs without prefixes or color codings
custom: customLogger
};
And use it anywhere like
sails.config.logging.custom.debug("winston mongodb transport logging");

Multiple log files with Winston?

We'd like to use Winston for our logging in Node.js. But, we can't figure out how to have two log files: one for just errors, and one for everything else.
Doing this the naive way doesn't work, however: adding multiple winston.transports.File transports gives an error.
Others have run into this problem, with vague hints of a solution, but no real answer.
Any ideas?
Unfortunately, the patch that pesho mentioned seems to be still not included in the official version (see stephenbeeson's comment in the pull request #149).
So, I used a workaround instead. As winston compares the name attributes, you can fool it by defining the name yourself:
winston = require 'winston'
logger = new winston.Logger
transports: [
new winston.transports.File
name: 'file#debug'
level: 'debug'
filename: '/tmp/debug.log'
new winston.transports.File
name: 'file#error'
level: 'error'
filename: '/tmp/error.log'
]
logger.error 'error' # both logs
logger.debug 'debug' # on debug log
Maybe not elegant, but at least it works.
In the meantime, you can implement a rudimentary wrapper using the same interface like so
var winston = require('winston');
var configs = require('./env.js');
var debug = new winston.Logger({
levels: {
debug: 0
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'debug'}),
new (winston.transports.Console)({level: 'debug'})
]
});
var info = new winston.Logger({
levels: {
info: 1
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'info'}),
new (winston.transports.Console)({level: 'info'})
]
});
var warn = new winston.Logger({
levels: {
warn: 2
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'warn'}),
new (winston.transports.Console)({level: 'warn'})
]
});
var error = new winston.Logger({
levels: {
error: 3
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'error'}),
new (winston.transports.Console)({level: 'error'})
]
});
var exports = {
debug: function(msg){
debug.debug(msg);
},
info: function(msg){
info.info(msg);
},
warn: function(msg){
warn.warn(msg);
},
error: function(msg){
error.error(msg);
},
log: function(level,msg){
var lvl = exports[level];
lvl(msg);
}
};
module.exports = exports;
This will cover the basic winston API. could be extended for metadata and so on...
I just sent a pull request that allows using multiple File transports in one logger.
https://github.com/flatiron/winston/pull/149
It is already merged into flatiron/winston.
You can also use my forked repo:
https://github.com/pdobrev/winston
You just need to give the transport a custom name property so you don't have a collision:
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ name: 'text', filename: logFile, json: false }),
new (winston.transports.File)({ name: 'json', filename: logFileJson })
]
});
You can read more about multiple transports in the docs: https://github.com/winstonjs/winston#multiple-transports-of-the-same-type
This feature is now officially supported in Winston and is addressed in the README here
Code example:
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}

Resources