Code Monkey home page Code Monkey logo

Comments (6)

Marak avatar Marak commented on May 23, 2024

What operating system on you on? Is it Windows? Also, what is your node -v and npm -v and microcule -v values returning on console?

The default behavior for console.log in microcule services should be to write directly to your console. This is the default logger that should being run: https://github.com/Stackvana/microcule/blob/master/lib/plugins/logger.js

It's also possible to easily override this logging behavior with your own custom logging middleware.

How are you starting the microcule server?

from microcule.

rishabh1952 avatar rishabh1952 commented on May 23, 2024

Hi, Yes i am using windows 10 OS with npm-5.6.0, microcule-6.0.0,node-v8.11.1. I tried both way to start microcule first npm start and second direct index file run like microcule ./index.js
Default logging(https://github.com/Stackvana/microcule/blob/master/lib/plugins/logger.js) is displaying my console ,
For custom looging in index.js am using console.log() that is not working is it possible to support
logging in console for index.js using console.log()?
I also tried for custom logging using witson library that also not printing logs in console.its printing in output please check below refrence and attached screenshots
Example:

module['exports'] = function calculateSal(req, res) {
  var flow = require('../setup.js');
  var logger = require('../logger.js'); //custom logger class using witson
  //console.log("input salary is:", req.params.salary); //Not working
  //console.log("input pf is:", req.params.pf); //Not working
  logger.info('input salary is:%s', req.params.salary);
  logger.info('input pf is:%s', req.params.pf);
  flow.totalSal(req.params.salary, req.params.pf).then(totalsal => {
    res.json({
      message: "Salray calculated successfully",
      salary: req.params.salary,
      PF: req.params.pf,
      totalSal: totalsal
    });
  }).catch(err => {
    res.json({
      errorMessage: "salary is not proper."
    });
  });
};

Here just one sample am trying calculate total salary with post json request using postman

**Request:** POST- content-type:(application/json)
 {
    "salary": 25000,
    "pf":1800
}

In this total sal function it just adding both salary+pf and return response.

**Response in postman in Text format**
---------------------------------------
2018-07-06T06:12:10.191Z  info: input salary is:25000
2018-07-06T06:12:10.199Z  info: input pf is:1800
{
  "message": "Salray calculated successfully",
  "salary": "25000",
  "PF": "1800",
  "totalSal": "26800"
}
**Note:**
2018-07-06T06:12:10.191Z  info: input salary is:25000
2018-07-06T06:12:10.199Z  info: input pf is:1800

1.These first two logger i want to print in console.we can print this in console?
2.Second thing am returning response as json(res.json()) why its not return me as json format.its giving me as text format..It is possible to get stdout response as json?
Postman Screenshots attached Please clarify me.
capture

from microcule.

Marak avatar Marak commented on May 23, 2024

To answer your questions:

  1. Yes, console.log is suppose to work as normal and I have confirmed it's working here on v6.1.0. I haven't had time to test Windows.

  2. Yes, microcule returns JSON. You have to use API methods as suggested in examples.

It looks like winston or maybe just console.log in general isn't working as intended in your environment. I suspect it's windows related. The output from the console statements are suppose to be redirected over STDIO and then to console by default. It looks like it's being redirected to client output instead.

Maybe try running the bare-bone examples instead of using microcule command, it could help.

see:

https://github.com/Stackvana/microcule/blob/master/examples/http-server-simple.js
https://github.com/Stackvana/microcule/blob/master/examples/express-simple.js

Would also be good if you could to test on non-windows machine.

from microcule.

rishabh1952 avatar rishabh1952 commented on May 23, 2024

Hi thanks for update I tried with non windows machine(linux OS ubuntu 16.04) there its solved the issues but console.log coming in second request.First am giving request that time default logging only displaying in my console and am getting res, then if i hit second time same request then default and custom logging displaying in my console.Is it a bug ? I am using node-v8.11.3 ,npm :5.0.0, microcule-6.0.0 version.Please check in your side and please let me now why first time custom log(console.log) is not displaying.If in your side log displaying in first time please update your npm,and node version so i can update same in my machine.

module.exports = function (req,res) {
res.write('Hello,This is javascript function\n');
console.log("Hello,This is javascript function");// working in second request in ubuntu
res.json(req.params); //getting response as Text format in postman
res.end('');
};

1)First time request custom log not displaying in console,second time same request working for this class in non-windows machine.
2) From this Js file if i call another js class function and there am adding console.log that also not displaying in console in ubuntu linux environment ,that log displaying in my output not console,why like this?
3)Here i am returning response as u suggested example res.json(req.params); Am getting this response as text format in postman you told microcule returns as json but in windows and non-windows machine same beheviour am getting, am not using express module.can i get this response as JSON format ?
Please check this screenshots
capture

from microcule.

rishabh1952 avatar rishabh1952 commented on May 23, 2024

Hi Above comment third point am able to resolve.I set output content type in microcule\lib\plugins\spawn\index.js file. so am getting response as json format.If possible clarify me first and second point.Thanks.

from microcule.

Marak avatar Marak commented on May 23, 2024

@rishabh1952 -

It would be best if you could use the latest version of microcule which is on Github, v6.1.0

Calling res.json should end the request so you shouldn't be calling res.end afterwards. You can set headers inside the service like:

  res.writeHead(200, { 'Content-Type': 'application/json' });  

res.json should automatically be setting the json content-type though. It would be good to add this into microcule test suite.

I'm not seeing any of the logging behavior acting strange here or in production. console.log is being overloaded in the context of the node function, so you might have run into issues with require scope somewhere. It should be working. It might be better for you to use separate logging function or to trace code paths for logging to see where it's being redirected wrong.

We have a good code coverage for hook.io which consumes microcule and serves it in production, but I'm looking at the nyc code coverage report for microcule itself and it's below 50%. It would be great if we could start adding more tests for these issues.

from microcule.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.