98

I'm super new to React and I'm trying to get it set up for Meteor and piecing stuff together from other sources too.

One of these other sources set up console logging for the app, but I'm going the ES6/JSX way, so just using their code wouldn't work for me (or it doesn't seem like it does).

Some code I found for logging is:

import Logger from 'simple-console-logger';
Logger.configure({level: 'debug'});

But I'm seeing this error:

Cannot find module './dumy'

I also tried using react-logger and react-console-logger to no avail. Here's my code for the latter, which I believe should work.

import {Logger, ConsoleLogger} from 'react-console-logger';
const myLogger = new Logger();
export default class App extends Component {
    render() {
        myLogger.info('something witty');
    }
}

However, myLogger.info('...') is making a call to node_modules/react-console-logger/lib/Logger.js which has it defined as

picture of code since copy-paste doesn't work

And this.logger is undefined, although I see it be defined above?

What am I doing wrong? It looks to me like the library has it wrong, but maybe it has something to do with me using JSX files instead of JavaScript?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
adinutzyc21
  • 1,538
  • 1
  • 11
  • 23
  • Do you need logging just to help debug / write your code or is this more of a permanent fixture? – patrick Nov 17 '16 at 05:39
  • Just to help debug. I can do without, but if I can't even get console logging to work, I'm not sure I can get anything to work... – adinutzyc21 Nov 17 '16 at 21:42
  • 1
    No need to reinvent the wheel. This is the best link that explains all. https://codeburst.io/react-native-debugging-tools-3a24e4e40e4 – HIRA THAKUR Dec 18 '17 at 09:19
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/40647361/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen Jul 02 '23 at 12:37

3 Answers3

132

If you're just after console logging here's what I'd do:

export default class App extends Component {
  componentDidMount() {
    console.log('I was triggered during componentDidMount')
  }

  render() {
    console.log('I was triggered during render')
    return ( 
      <div> I am the App component </div>
    )
  }
}

Shouldn't be any need for those packages just to do console logging.

patrick
  • 9,837
  • 3
  • 20
  • 27
  • 11
    You can even add styles to your console.log message. Try this `console.log('%c color message', 'color: #f0c002')` – kkkkkkk Nov 18 '16 at 05:15
  • Its no doubt fantastic answer but I'm getting warning: `Unexpected console statement no-console` – Adil Feb 01 '17 at 04:51
  • 2
    @adi that would be likely caused by ESLint - http://eslint.org/docs/rules/no-console – patrick Feb 01 '17 at 05:20
  • 5
    I would be concerned to use console.log all over my app. I'd rather use something similar to what @adinutzyc21 was trying to use, as using some logging component that allows to control the log level for an app is generally good practice. For my project I used [watson/console-log-level](https://github.com/watson/console-log-level) and I didn't run into any issues using that. – Rafał Nowosielski Feb 06 '17 at 10:19
  • 2
    @RafałNowosielski Generally I only use `console.log` while debugging a specific issue and then they are removed. ESLint also has a rule which enforces removing them. If there is a need for additional logging / monitoring then some other way would be definitely recommended. – patrick Feb 07 '17 at 23:28
  • 3
    To add to Rafals comment, I was able to use the watson console-log-level component like the following: `import Logger from 'console-log-level'; let log = Logger({level: 'trace'}); ` See https://github.com/watson/console-log-level – Eric Aug 23 '17 at 10:23
  • This is not shown in the console. No filters applied. – Soerendip Sep 15 '21 at 21:04
  • To make the example complete, put it in `src/App.js` and add two imports `import React from 'react';import { Component } from "react";` – Timo Aug 21 '22 at 08:47
126

Here are some more console logging "pro tips":

console.table

var animals = [
    { animal: 'Horse', name: 'Henry', age: 43 },
    { animal: 'Dog', name: 'Fred', age: 13 },
    { animal: 'Cat', name: 'Frodo', age: 18 }
];

console.table(animals);

console.table

console.trace

Shows you the call stack for leading up to the console.

console.trace

You can even customise your consoles to make them stand out

console.todo = function(msg) {
    console.log(‘ % c % s % s % s‘, ‘color: yellow; background - color: black;’, ‘–‘, msg, ‘–‘);
}

console.important = function(msg) {
    console.log(‘ % c % s % s % s’, ‘color: brown; font - weight: bold; text - decoration: underline;’, ‘–‘, msg, ‘–‘);
}

console.todo(“This is something that’ s need to be fixed”);
console.important(‘This is an important message’);

console.todo

If you really want to level up don't limit your self to the console statement.

Here is a great post on how you can integrate a chrome debugger right into your code editor!

https://hackernoon.com/debugging-react-like-a-champ-with-vscode-66281760037

user1095118
  • 4,338
  • 3
  • 26
  • 22
21

If you want to log inside JSX, you can create a dummy component
which plugs where you wish to log:

// Some component with JSX and a logger inside
const App = () =>
  <ul>
    {[...Array(4)].map((v, i) => (
      console.log(i), <li key={i}>{i+1}</li>
      )
    )}
    {console.log(123)}
  </ul>

// Render
ReactDOM.render(<App />, root)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Or for fun, as a Component:

const Console = prop => (
  console[Object.keys(prop)[0]](...Object.values(prop))
  ,null // ➜ React components must return something
)

// Some component with JSX and a logger inside
const App = () =>
  <div>
    <p>imagine this is some component</p>
    <Console log='foo' />
    <p>imagine another component</p>
    <Console warn='bar' />
  </div>

// Render
ReactDOM.render(
  <App />,
  document.getElementById("react")
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vsync
  • 118,978
  • 58
  • 307
  • 400