log4js icon indicating copy to clipboard operation
log4js copied to clipboard

Lack of document on how to configure and use log4js

Open jvchbabu opened this issue 9 years ago • 23 comments

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

jvchbabu avatar Sep 10 '15 07:09 jvchbabu

I came here with the exact same issue. The logger for internal use in Log4js can't be created.

DenEwout avatar Sep 15 '15 11:09 DenEwout

I have the same error here.

oussama-benmahmoud avatar Dec 09 '15 11:12 oussama-benmahmoud

Even after building the the log4js.js file I had this error Uncaught ReferenceError: log4jsLogger is not defined

oussama-benmahmoud avatar Dec 09 '15 14:12 oussama-benmahmoud

same here

waynejuckts avatar Dec 10 '15 12:12 waynejuckts

I get the same problem. Log4js is not populated with properties.

mrafaqi avatar Dec 17 '15 13:12 mrafaqi

Has anyone had a solution for this?

venkatsep14 avatar Jan 31 '16 22:01 venkatsep14

I got it working by building it with grunt.

  1. cd into log4js subdirectory
  2. npm install
  3. grunt build
  4. include the target/log4js.min.js file in your project

tjwelde avatar Feb 03 '16 11:02 tjwelde

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

keltik85 avatar Feb 03 '16 13:02 keltik85

Still got this issue.:(

nangsaigon avatar Feb 26 '16 12:02 nangsaigon

Why use a logger with Javascript anyways? Firebug and Chrome have the best debugging tools!

keltik85 avatar Feb 26 '16 12:02 keltik85

With the client log, we may troubleshoot more quickly. We have a project here requires to output client side log to file.

nangsaigon avatar Feb 26 '16 12:02 nangsaigon

I came here for the same issue. Is even this project(repository) alive?

jinahya avatar Mar 29 '16 03:03 jinahya

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.

kimballjohnson avatar Mar 29 '16 21:03 kimballjohnson

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.

uthaman avatar Nov 04 '16 12:11 uthaman

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

Sae1962 avatar Mar 29 '17 10:03 Sae1962

Please read " goldquest commented on 3 Feb 2016 ". It works for me. Thanks goldquest

EpsilonBoo avatar May 11 '17 14:05 EpsilonBoo

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.

stritti avatar Sep 05 '17 22:09 stritti

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 avatar Sep 20 '17 17:09 RotateAt60MPH

@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

stritti avatar Sep 21 '17 06:09 stritti

Ok I figured out the issue with "getLogger() is not a function" and how to implement it correctly.

  1. 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);
  }
}
  1. 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."

telangn avatar Oct 16 '18 04:10 telangn

Hi followed https://www.npmjs.com/package/log4js and created two files

  1. 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" } } }`

  2. 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

vijayjdk avatar Dec 05 '19 06:12 vijayjdk

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

vijayjdk avatar Dec 26 '19 10:12 vijayjdk

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.

WGPuckett avatar Apr 14 '21 18:04 WGPuckett