log4js
log4js copied to clipboard
Lack of document on how to configure and use log4js
In order to use log4js framework. I downloaded and followed the instructions mentioned at http://stritti.github.io/log4js/. But could n't able proceed further as logger initialization itself failed. Error i got when tried to create a logger is below. Uncaught TypeError: Log4js.Logger is not a function(…). Please help us to use log4js
I came here with the exact same issue. The logger for internal use in Log4js can't be created.
I have the same error here.
Even after building the the log4js.js file I had this error Uncaught ReferenceError: log4jsLogger is not defined
same here
I get the same problem. Log4js is not populated with properties.
Has anyone had a solution for this?
I got it working by building it with grunt.
-
cd
into log4js subdirectory -
npm install
-
grunt build
- include the
target/log4js.min.js
file in your project
same issue, here chrome log:
Uncaught TypeError: Log4js.Logger is not a functionLog4js.getLogger @ log4js.js:85(anonymous function) @ logger-test.html:7 logger-test.html:14 Uncaught TypeError: Cannot read property 'info' of undefined(anonymous function) @ logger-test.html:14
Still got this issue.:(
Why use a logger with Javascript anyways? Firebug and Chrome have the best debugging tools!
With the client log, we may troubleshoot more quickly. We have a project here requires to output client side log to file.
I came here for the same issue. Is even this project(repository) alive?
I believe that Log4Js presumes some dependencies that are installed when the installation is in a node project. especially when installed by npm or bower. however, I'm using it in a basic web site, web application and it is hanging. I'm going to try log4javascript.
Modify Gruntfile.js to include log4js-all.js and build.
concat: { build: { src: ["src/main/js/log4js.js", "src/main/js/level.js", "src/main/js/logger.js", "src/main/js/custom-event.js", "src/main/js/logging-event.js", "src/main/js/appender.js", "src/main/js/appenders/*.js", "src/main/js/layout.js", "src/main/js/layouts/*.js", "src/main/js/date-formatter.js", "src/main/js/fifo-buffer.js", "src/main/js/log4js-all.js"], dest: "target/files/<%= pkg.name %>/js/<%= pkg.name %>.combined.js" } },
It should work now, took a while to figure this out.
For a newbie for log4js, the information here is clearly not working. It is written both in the documentation above & the only library file, log4js.js, that this file is the only file needed for logging. However, when I try the logger example on the page above, I get the following errors:
Uncaught TypeError: Log4js.Logger is not a constructor at new getLogger (log4js.js:85) at logger.html:7 getLogger @ log4js.js:85 (anonymous) @ logger.html:7 logger.html:15 Uncaught TypeError: Cannot read property 'info' of undefined at logger.html:15
Attached, you see the top directory of the test project. Logging.zip
Please read " goldquest commented on 3 Feb 2016 ". It works for me. Thanks goldquest
To build the JavaScript library npm and grunt is used:
cd log4js
npm install
grunt build
Include then include the target/log4js.min.js
file in your project.
All the prereqs make this library infeasible for some servers. Needing to install node.js, jsdoc, and JAVA? All this just to run grunt to get this library 'built'? That's pretty heavy weight.
@RotateAt60MPH you are right. There are combined several Ideas within one project. Nowadays I would have splitted in separate projects.
Currently I have less time to do but I am open for PRs
Ok I figured out the issue with "getLogger() is not a function" and how to implement it correctly.
- Create a js file (I called it log4js.js) with this in it:
'use strict';
var log4js = require('log4js');
log4js.configure({
appenders: {
protractorLogs: {
type: 'file',
filename: 'protractor.log'
},
console: {
type: 'console'
}
},
categories: {
protractor: {
appenders: ['protractorLogs'],
level: 'error'
},
another: {
appenders: ['console'],
level: 'trace'
},
default: {
appenders: ['console', 'protractorLogs'],
level: 'trace'
}
}
});
module.exports = {
log: function (text) {
var logger = log4js.getLogger('protractor');
logger.error(text);
}
}
- In your second js file add the following:
var log4js = require("../utilities/log4js");
log4js.log('This is the beginning of my test');
Let me know if this works for anybody else as it has for me with getting passed the whole "getLogger() is not a function."
Hi followed https://www.npmjs.com/package/log4js and created two files
-
log4js.json `{ "appenders": { "console": { "type": "console", "category": "console" },
"file": { "category": "test-file-appender", "type": "file", "filename": "./reports/log_info.log", "fileAppend": "false", "maxLogSize": 10240, "backups":3, "pattern": "%d{dd/MM hh:mm} %-5p %m" }
}, "categories": { "default": { "appenders": ["console", "file"], "level": "DEBUG" }, "file": { "appenders": ["file"], "level": "DEBUG" } } }`
-
log4jsconfig.js `var log4jsConfig = function() { var log4js = require('log4js'); log4js.configure('./config/log4js.json'); let logger = log4js.getLogger("file"); // this is the category of logging (file/console)
this.logger = function() { return logger; } };
module.exports = new log4jsConfig();`
and in my test case used
logger.debug('Message here..!');
and its working perfectely
Hi, I am using https://github.com/stritti/log4js/issues/15#issuecomment-561985908
the approach i am able to log in to file and console as well. However, the text is not appended with the existing text. Every time previous text is removed please suggest me any way to fix this.
I also tried "fileAppend": "true", but its not working too.
Thanks, Vijay Kumar
To build the JavaScript library npm and grunt is used:
cd log4js npm install grunt build
Include then include the
target/log4js.min.js
file in your project.
It gets me past the first errors. But still doesn't get it working.