ostrio:logger no client logs - node.js

i have initialized the logger like in the docs: https://atmospherejs.com/ostrio/logger
with a transport to mongodb: https://atmospherejs.com/ostrio/loggermongo#initialization-isomorphic
import { Logger } from 'meteor/ostrio:logger';
import { LoggerMongo } from 'meteor/ostrio:loggermongo';
export const idLogger = new IdLogger();
export function addMongoDbLogging(idLogger, filters) {
(new LoggerMongo(idLogger , {
collection: AppLogs,
})).enable({
enable: true,
filter: filters,
client: true,
server: true,
});
}
The code lies is in the imports directory
And the call to is in the server directory during meteor startup.
if(Meteor.isServer) {
initLogger(); // calls addMongoDbLogging();
}
On The server logging works with no problems. All logs are written in the AppLogs Collection.
But when i try to log something from the client... nothing happens.
Same behavior is for other transports like console. See: https://atmospherejs.com/ostrio/loggerconsole
Serverside all logs show up in my console. Client logs are not shown on neither server or client consoles.
Can someone tell me what's wrong here?
Update: It could be that the server/startup is not the right place for isomorphic code. Where should this be put?

Ok i found the problem.
It was the location of the code.
It has to be done in the /lib folder.

Related

Creating sub connections with azure-mobile-apps and NodeJS

I'm trying to create an API using nodeJS, express and azure-mobile-apps to do some data synchronisation between an Ionic3 mobile app (which use an SQLite local database) and a Microsoft SQL Database.
My API has to create a synchronisation connection for each mobile application. Each application will be linked to a distant database. For example, if user_01 wants to synchronise his data, he's going to be linked to his client_01 database. So each time it'll have to, the API will create a new process running on a different port.
here is an example : https://zupimages.net/up/19/36/szhu.png
The problem is that i'm not able to create more than one connection with azure-mobile-apps. The first one always works, but the second, third etc are still using the first connection that i have instantiated. I've looked into the app stack and everything seems fine.
Is that an issue with azure-mobile-app, or did I misunderstand something with express ?
Thanks for your responses !
var azureMobileApps = require('azure-mobile-apps');
var express = require('express');
module.exports = {
async createConnection(client) {
try {
let app = express();
mobileApp = azureMobileApps({
homePage: true,
swagger: true,
data: {
server: '****',
user: client.User,
password: client.Password,
port: '1443',
database: client.Database,
provider: 'mssql',
dynamicSchema: false,
options: {
encrypt: false
}
}
});
await mobileApp.tables.import('./tables');
await mobileApp.tables.initialize();
app.listen(global.portCounter);
app.use(mobileApp);
console.log(app._router.stack);
console.log('Listening on port ',global.portCounter);
global.portCounter++;
} catch (error) {
console.log(error);
}
}
}
It's working now. The thing is, it's impossible to do multiple connection with the azure-mobile-apps SDK for nodeJS.
I had to use worker-thread which seems to isolate the memory in a sub-proccess.
Hope it can help somebody one day

App Object in Electron Module and getAppPath throws Error

I have a strange problem with my application. I get an error, and I can't solve it. First at all, I installed a new project, so everything is clean. Someone sent me this repo to use for an Angular, Electron and Nodejs Application. Everything worked fine, but then I decided to install an embedded database like sqlite3. For this I found NeDB, and the module is perfect for my needs. First I had the problem, has nothing to do with my general problem, that I can't create a database file. So I read that I can only create files in my application path, because something about Electron and that's working in a browser.
I found the getAppPath() method that is implemented in my app object from the Electron module. Here starts the problem. For hours I tried to get the application path from this object. Finally, I wrote this code.
import { Injectable } from '#angular/core';
var nedb = require('nedb');
import { app } from 'electron';
import * as path from 'path';
#Injectable()
export class DatabaseService {
app: typeof app;
Database: any;
constructor() {
this.app = window.require("electron").app;
this.Database = new nedb({ filename: path.join(this.app.getAppPath(), '/diary.db'), autoload: true, timestampData: true });
var scott = {
name: 'Scott',
twitter: '#ScottWRobinson'
};
this.Database.insert(scott, function(err, doc) {
console.log('Inserted', doc.name, 'with ID', doc._id);
});
}
}
And I get this error.
I found this posting, but I don't really understand what the post is trying to tell me. I followed the links, but nothing seems to help. Anyone have an idea?

Hapi.js Catbox Redis returning "server.cache is not a function"

So I'm like 99% sure I'm just screwing up something dumb here.
I'm trying to set up catbox to cache objects to redis. I have redis up and running and I can hit it with RDM (sql pro like utility for redis) but Hapi is not cooperating.
I register the redis catbox cache like so:
const server = new Hapi.Server({
cache: [
{
name: 'redisCache',
engine: require('catbox-redis'),
host: 'redis',
partition: 'cache',
password: 'devpassword'
}
]
});
I am doing this in server.js After this block of code I go on to register some more plugins and start the server. I also export the server at the end of the file
module.exports = server;
Then in my routes file, I am attempting to set up a testing route like so:
{
method: 'GET',
path: '/cacheSet/{key}/{value}',
config: { auth: false },
handler: function(req, res) {
const testCache = server.cache({
cache: 'redisCache',
expireIn: 1000
});
testCache.set(req.params.key, req.params.value, 1000, function(e) {
console.log(e);
res(Boom.create(e.http_code, e.message));
})
res(req.params.key + " " + req.params.value);
}
},
Note: My routes are in an external file, and are imported into server.js where I register them.
If I comment out all the cache stuff on this route, the route runs fine and returns my params.
If I run this with the cache stuff, at first I got "server not defined". So I then added
const server = require('./../server.js');
to import the server.
Now when I run this, I get "server.cache is not a function" and a 500 error.
I don't understand what I'm doing wrong. My guess is that I'm importing server, but perhaps it's the object without all the configs set so it's unable to use the .cache method. However this seems wrong because .cache should always be a default method with the default memory cache, so even if my cache registration isn't active yet, server.cache should theoretically still be a method.
I know it has to be something basic I'm messing up, but what?
I was correct. I was doing something stupid. It had to do with how I was exporting my server. I modified my structure to pull out the initial server creation and make it more modular. Now I am simply exporting JUST the server like so:
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server({
cache: [
{
name: 'redisCache',
engine: require('catbox-redis'),
host: 'redis',
partition: 'cache',
password: 'devpassword'
}
]
});
module.exports = server;
I then import that into my main server file (now index.js previously server.js) and everything runs fine. I can also import this into any other file (in this case my routes file) and access the server for appropriate methods.
Redis is happily storing keys and Hapi is happily not giving me errors.
Leaving here in case anyone else runs into a dumb mistake like this.

Custom Logger in SailsJS

Below is my log.js file but it only logs "Hello from colorized winston", but it's not logging all my sails.log.info in my app. What am I doing wrong. I searched everywhere and can't figure it out. All I found is this https://groups.google.com/forum/#!topic/sailsjs/67u7SqzsNJQ and it seems to confirm I'm doing correctly.
var winston = require('winston'),
Papertrail = require('winston-papertrail').Papertrail;
var logger = new winston.Logger({
transports: [
new Papertrail({
host: 'logs3.papertrailapp.com',
port: xxxxx, // my port here
colorize: true
})
]
});
logger.info('Hello from colorized winston', logger);
module.exports = {
log: {
custom: logger
}
};
Any help would greatly be appreciated.
PS. I'm jumping into a project created by someone else so it's possible they broke something. If someone can give me a lead how to debug by telling me how custom works that would also be appreciated.
Make sure you've setup the correct logging level. In your config/log.js file, set the log level to something like level: silly. This should log pretty much everything.

Node.js Logging

Is there any library which will help me to handle logging in my Node.Js application? All I want to do is, I want to write all logs into a File and also I need an options like rolling out the file after certain size or date.
I have incorporated log4js im trying to keep all the configuration details in one file and use only the methods in other application files for ease of maintenance. But it doesnt work as expected. Here is what I'm trying to do
var log4js = require('log4js');
log4js.clearAppenders()
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('test.log'), 'test');
var logger = log4js.getLogger('test');
logger.setLevel('ERROR');
var traceLogger = function (message) {
logger.trace('message');
};
var errorLogger = function (message) {
logger.trace(message);
};
exports.trace = traceLogger;
exports.error = errorLogger;
I have included this file in other files and tried
log.error ("Hello Error Message");
But it is not working. Is there anything wrong in this ?
Winston is a pretty good logging library. You can write logs out to a file using it.
Code would look something like:
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/debug.log', json: false })
],
exceptionHandlers: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
],
exitOnError: false
});
module.exports = logger;
You can then use this like:
var logger = require('./log');
logger.info('log to file');
Scribe.JS Lightweight Logger
I have looked through many loggers, and I wasn't able to find a lightweight solution - so I decided to make a simple solution that is posted on github.
Saves the file which are organized by user, date, and level
Gives you a pretty output (we all love that)
Easy-to-use HTML interface
I hope this helps you out.
Online Demo
http://bluejamesbond.github.io/Scribe.js/
Secure Web Access to Logs
Prints Pretty Text to Console Too!
Web Access
Github
https://github.com/bluejamesbond/Scribe.js
Log4js is one of the most popular logging library for nodejs application.
It supports many cool features:
Coloured console logging
Replacement of node's console.log functions (optional)
File appender, with log rolling based on file size
SMTP, GELF, hook.io, Loggly appender
Multiprocess appender (useful when you've got worker processes)
A logger for connect/express servers
Configurable log message layout/patterns
Different log levels for different log categories (make some parts
of your app log as DEBUG, others only ERRORS, etc.)
Example:
Installation: npm install log4js
Configuration (./config/log4js.json):
{"appenders": [
{
"type": "console",
"layout": {
"type": "pattern",
"pattern": "%m"
},
"category": "app"
},{
"category": "test-file-appender",
"type": "file",
"filename": "log_file.log",
"maxLogSize": 10240,
"backups": 3,
"layout": {
"type": "pattern",
"pattern": "%d{dd/MM hh:mm} %-5p %m"
}
}
],
"replaceConsole": true }
Usage:
var log4js = require( "log4js" );
log4js.configure( "./config/log4js.json" );
var logger = log4js.getLogger( "test-file-appender" );
// log4js.getLogger("app") will return logger that prints log to the console
logger.debug("Hello log4js");// store log in file
You can also use npmlog by issacs, recommended in
https://npmjs.org/doc/coding-style.html.
You can find this module here
https://github.com/isaacs/npmlog
The "logger.setLevel('ERROR');" is causing the problem. I do not understand why, but when I set it to anything other than "ALL", nothing gets printed in the file. I poked around a little bit and modified your code. It is working fine for me. I created two files.
logger.js
var log4js = require('log4js');
log4js.clearAppenders()
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('test.log'), 'test');
var logger = log4js.getLogger('test');
logger.setLevel('ERROR');
var getLogger = function() {
return logger;
};
exports.logger = getLogger();
logger.test.js
var logger = require('./logger.js')
var log = logger.logger;
log.error("ERROR message");
log.trace("TRACE message");
When I run "node logger.test.js", I see only "ERROR message" in test.log file. If I change the level to "TRACE" then both lines are printed on test.log.
Winston is strong choice for most of the developers. I have been using winston for long. Recently I used winston with with papertrail which takes the application logging to next level.
Here is a nice screenshot from their site.
How its useful
you can manage logs from different systems at one place. this can be very useful when you have two backend communicating and can see logs from both at on place.
Logs are live. you can see realtime logs of your production server.
Powerful search and filter
you can create alerts to send you email if it encounters specific text in log.
and you can find more http://help.papertrailapp.com/kb/how-it-works/event-viewer/
A simple configuration using winston,winston-express and winston-papertrail node modules.
import winston from 'winston';
import expressWinston from 'express-winston';
//
// Requiring `winston-papertrail` will expose
// `winston.transports.Papertrail`
//
require('winston-papertrail').Papertrail;
// create winston transport for Papertrail
var winstonPapertrail = new winston.transports.Papertrail({
host: 'logsX.papertrailapp.com',
port: XXXXX
});
app.use(expressWinston.logger({
transports: [winstonPapertrail],
meta: true, // optional: control whether you want to log the meta data about the request (default to true)
msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
colorize: true, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response
}));
A 'nodejslogger' module can be used for simple logging. It has three levels of logging (INFO, ERROR, DEBUG)
var logger = require('nodejslogger')
logger.init({"file":"output-file", "mode":"DIE"})
D : Debug, I : Info, E : Error
logger.debug("Debug logs")
logger.info("Info logs")
logger.error("Error logs")
The module can be accessed at : https://www.npmjs.com/package/nodejslogger
Observe that errorLogger is a wrapper around logger.trace. But the level of logger is ERROR so logger.trace will not log its message to logger's appenders.
The fix is to change logger.trace to logger.error in the body of errorLogger.
Each answer is 5 6 years old, so bit outdated or depreciated. Let's talk in 2020.
simple-node-logger is simple multi-level logger for console, file, and rolling file appenders. Features include:
levels: trace, debug, info, warn, error and fatal levels (plus all and off)
flexible appender/formatters with default to HH:mm:ss.SSS LEVEL message
add appenders to send output to console, file, rolling file, etc
change log levels on the fly
domain and category columns
overridable format methods in base appender
stats that track counts of all log statements including warn, error, etc
You can easily use it in any nodejs web application:
// create a stdout console logger
const log = require('simple-node-logger').createSimpleLogger();
or
// create a stdout and file logger
const log = require('simple-node-logger').createSimpleLogger('project.log');
or
// create a custom timestamp format for log statements
const SimpleNodeLogger = require('simple-node-logger'),
opts = {
logFilePath:'mylogfile.log',
timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
},
log = SimpleNodeLogger.createSimpleLogger( opts );
or
// create a file only file logger
const log = require('simple-node-logger').createSimpleFileLogger('project.log');
or
// create a rolling file logger based on date/time that fires process events
const opts = {
errorEventName:'error',
logDirectory:'/mylogfiles', // NOTE: folder must exist and be writable...
fileNamePattern:'roll-<DATE>.log',
dateFormat:'YYYY.MM.DD'
};
const log = require('simple-node-logger').createRollingFileLogger( opts );
Messages can be logged by
log.info('this is logged info message')
log.warn('this is logged warn message')//etc..
PLUS POINT: It can send logs to console or socket. You can also append to log levels.
This is the most effective and easy way to handle logs functionality.
Here is lightweight module for logging data with full stack trace
#grdon/logger
const logger = require('#grdon/logger')({
defaultLogDirectory : __dirname + "/logs",
})
// ...
logger(someParams, 'logfile.txt')
logger(anotherParams, 'anotherLogFile.log')

Resources