<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Delila</title>
    <description>The latest articles on DEV Community by Delila (@deliladlee).</description>
    <link>https://dev.to/deliladlee</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F208245%2F3bb7f006-80c8-47e8-abb6-345af0c3c18a.png</url>
      <title>DEV Community: Delila</title>
      <link>https://dev.to/deliladlee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deliladlee"/>
    <language>en</language>
    <item>
      <title>Getting Started Quickly With React Logging</title>
      <dc:creator>Delila</dc:creator>
      <pubDate>Wed, 07 Aug 2019 15:23:28 +0000</pubDate>
      <link>https://dev.to/scalyr/getting-started-quickly-with-react-logging-2c89</link>
      <guid>https://dev.to/scalyr/getting-started-quickly-with-react-logging-2c89</guid>
      <description>&lt;p&gt;Throughout this series, we've covered logging in &lt;a href="https://www.scalyr.com/blog/get-started-quickly-csharp-logging/" rel="noopener noreferrer"&gt;C#&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/get-started-quickly-java-logging" rel="noopener noreferrer"&gt;Java&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/started-quickly-python-logging" rel="noopener noreferrer"&gt;Python&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/getting-started-quickly-ruby-logging" rel="noopener noreferrer"&gt;Ruby&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/getting-started-quickly-with-go-logging/" rel="noopener noreferrer"&gt;Go&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/get-started-quickly-javascript-logging" rel="noopener noreferrer"&gt;JavaScript&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/getting-started-quickly-with-php-logging/" rel="noopener noreferrer"&gt;PHP&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/getting-started-swift-logging" rel="noopener noreferrer"&gt;Swift&lt;/a&gt;, and &lt;a href="https://www.scalyr.com/blog/get-started-scala-logging" rel="noopener noreferrer"&gt;Scala&lt;/a&gt;. We've also explored a few libraries and platforms, such as &lt;a href="https://www.scalyr.com/blog/log4j2-configuration-detailed-guide/" rel="noopener noreferrer"&gt;Log4j&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/getting-started-quickly-node-js-logging/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/started-quickly-spring-boot-logging/" rel="noopener noreferrer"&gt;Spring Boot&lt;/a&gt;, &lt;a href="https://www.scalyr.com/blog/rails-logger/" rel="noopener noreferrer"&gt;Rails&lt;/a&gt;, and &lt;a href="https://www.scalyr.com/blog/getting-started-angular-logging" rel="noopener noreferrer"&gt;Angular&lt;/a&gt;. Today we're going to be looking at logging in React.&lt;/p&gt;

&lt;p&gt;In this article, we'll&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up a simple React application&lt;/li&gt;
&lt;li&gt;Work through an example of simple logging&lt;/li&gt;
&lt;li&gt;Discuss what logging is and why it's important&lt;/li&gt;
&lt;li&gt;Explore some other logging methods that are available to us.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085441%2FReact_atom_image_with_scalyr_colors.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085441%2FReact_atom_image_with_scalyr_colors.png" alt="React_atom_image_with_scalyr_colors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Some Background Information&lt;/h2&gt;

&lt;p&gt;React, also known as React.js or React JS, is a popular front-end development library for creating user interfaces. It was originally developed for Facebook and released in 2013.&lt;/p&gt;

&lt;p&gt;React uses JavaScript as its core language. It allows for all the interactiveness we would expect from a JavaScript application, but with increased efficiency through something called a virtual DOM. At a very basic level, this allows only those components that have been affected to reload when changes are made, rather than the entire page. It does this by using an in-memory cache to compare a DOM object to its corresponding virtual DOM object and re-rendering only those that are different.&lt;/p&gt;

&lt;p&gt;Because React is a JavaScript library, this means that we once again run into the issues of client-side logging, as we saw when we covered &lt;a href="https://www.scalyr.com/blog/get-started-quickly-javascript-logging" rel="noopener noreferrer"&gt;JavaScript&lt;/a&gt;. We'll review this again after we've set up our simple logging application.&lt;/p&gt;

&lt;h2&gt;A Simple React Application&lt;/h2&gt;

&lt;p&gt;To create the simplest possible React application, we'll be using Facebook's &lt;a href="https://github.com/facebook/create-react-app#creating-an-app" rel="noopener noreferrer"&gt;Create React App&lt;/a&gt;. Before we get started though, let's make sure we have all the necessary tools.&lt;/p&gt;

&lt;h3&gt;Setting Up Our Package Manager&lt;/h3&gt;

&lt;p&gt;In order to use Create React App, you'll need a package manager, such as npx, npm, or Yarn. In this tutorial, we'll be using npm, but any one of these three options will work.&lt;/p&gt;

&lt;p&gt;If you're not sure whether you already have npm installed, open up a command line tool such as the Windows Command Prompt or PowerShell (Windows) or the Terminal app (MacOS). Use this command:&lt;/p&gt;

&lt;pre&gt;npm -v&lt;/pre&gt;

&lt;p&gt;If npm is already installed, you'll get a version number back. For example, 6.5.0 is the version number that was returned when I ran the command, and it's the version I'm using for this tutorial. If you get something similar back, you're good to go!&lt;/p&gt;

&lt;p&gt;If you don't get a version number back, you'll need to install npm first. Check out the &lt;a href="https://www.npmjs.com/get-npm" rel="noopener noreferrer"&gt;npm website&lt;/a&gt; to get the official instructions. In short, you'll need to download an installer from the &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; website and run it. This is because npm is distributed with Node.js. Once you've completed the installation, simply restart your computer and you're all set!&lt;/p&gt;

&lt;h3&gt;Downloading the Create React App&lt;/h3&gt;

&lt;p&gt;Now that we have npm, we're ready to create our React application. In your command line tool, you'll enter the following command:&lt;/p&gt;

&lt;pre&gt;npm init react-app my-app&lt;/pre&gt;

&lt;p&gt;This downloads the code we need from the Create React App page linked above and places it inside a directory called &lt;strong&gt;my-app&lt;/strong&gt;. You'll find the new directory inside your current directory.&lt;/p&gt;

&lt;p&gt;Let's see what it looks like before we make any new changes. Use this command:&lt;/p&gt;

&lt;pre&gt;cd my-app&lt;/pre&gt;

&lt;p&gt;And that will move the application into the newly created folder. Once inside, you can run the application by using the following:&lt;/p&gt;

&lt;pre&gt;npm start&lt;/pre&gt;

&lt;p&gt;Next, you'll need to open up a browser window and navigate to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;&lt;strong&gt;http://localhost:3000&lt;/strong&gt;&lt;/a&gt;. You'll be able to see your application running locally. It should have some text and a spinning React logo. Not a lot going on here, but we've got all the groundwork set up to do some simple logging!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085538%2F011-1024x660.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085538%2F011-1024x660.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want some more detailed information on how Create React App works, you can check out the README &lt;a href="https://github.com/facebook/create-react-app#creating-an-app" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Logging to the Console&lt;/h2&gt;

&lt;p&gt;The simplest way to log in React is by printing out to the console. Inside the new &lt;strong&gt;my-app&lt;/strong&gt; folder you should see another folder named &lt;strong&gt;src&lt;/strong&gt;. Inside, you'll see a file named &lt;strong&gt;App.js&lt;/strong&gt;. Open it now inside the IDE or text editor of your choice. As expected, it's very short, with just a div containing a header with an image, a paragraph, and a link. We won't bother with changing any of the existing code right now, but this is where you'd start if you wanted to expand this simple app into something more complex. Instead, we're just going to add a button underneath the link by adding the following code:&lt;/p&gt;

&lt;pre&gt;&amp;lt;div&amp;gt;
  &amp;lt;button onClick={this.logSomething}&amp;gt;Log Something!&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;

&lt;p&gt;Don't forget to add the corresponding function outside the render block.&lt;/p&gt;

&lt;pre&gt;logSomething = () =&amp;gt; {
  console.log("Button was clicked.")
}&lt;/pre&gt;

&lt;p&gt;Here's what your finished &lt;strong&gt;App.js&lt;/strong&gt; file should look like once you're done:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085616%2F021-969x1024.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085616%2F021-969x1024.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll notice that the app inside your browser updates as soon as you save your changes. It should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085642%2F03-1024x604.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085642%2F03-1024x604.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To open the console, right-click inside the application window, click &lt;strong&gt;Inspect&lt;/strong&gt;, and select the &lt;strong&gt;Console&lt;/strong&gt; tab. You might also be able to use the F12 key to do the same thing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085707%2F04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085707%2F04.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the console open, you'll be able to see anything that gets printed out when the button is pressed. Click on the button now, and you should see a message printed out into the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085822%2F05-1024x369.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085822%2F05-1024x369.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And there you have it! You've created a simple logging application in React!&lt;/p&gt;

&lt;h2&gt;What's Logging?&lt;/h2&gt;

&lt;p&gt;Before we go any further, let's make sure we have a solid understanding of what we mean when we talk about logging.&lt;/p&gt;

&lt;p&gt;At the &lt;a href="https://www.scalyr.com/blog/get-started-quickly-csharp-logging/" rel="noopener noreferrer"&gt;very beginning of this series&lt;/a&gt;, we defined logging, or application logging, as follows:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Application logging involves recording information about your application's runtime behavior to a more persistent medium.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;There are two things to note here. The first is that logging provides us with information about what the application does as it's running.&lt;/p&gt;

&lt;p&gt;The second is that we want our logs to stick around for a while rather than disappearing immediately. The level of persistence we need may vary depending on the application, but at the very least we want to be able to read the logs even if the application crashes or the server goes down.&lt;/p&gt;

&lt;p&gt;Now that we know what logging is, let's make sure we understand why we would want to go to all the trouble of adding logging to our applications.&lt;/p&gt;

&lt;h2&gt;What's the Motivation for Logging?&lt;/h2&gt;

&lt;p&gt;Once our software is out in the world and away from the safe confines of our local debug environment, we need a way to investigate issues and analyze performance. Put simply, logging allows us to record the behavior of our application and analyze it later. With so many things that could go wrong, logged data allows us to pinpoint what error occurred and even retrace the steps the user took to create or discover the issue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29091319%2FLogging-provides-us-with-a-strategy-for-maintaining-the-health-of-our-application.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29091319%2FLogging-provides-us-with-a-strategy-for-maintaining-the-health-of-our-application.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Furthermore, by logging data and analyzing it later, we become familiar with what normal behavior looks like and may even be able to detect and prevent problems before they occur. Logging—and more importantly, logging the right data—provides us with a strategy for maintaining the health of our application.&lt;/p&gt;

&lt;h2&gt;What Should We Be Logging?&lt;/h2&gt;

&lt;p&gt;Now that we know why logging is important, we need to decide what information to include. Randomly dumping data into a log file makes finding the data we actually need tedious and difficult. On the other hand, we want to provide enough information so that we can efficiently solve issues that arise. So what should we be logging?&lt;/p&gt;

&lt;p&gt;We can start by conceptually thinking of each log entry as an event. An event is something of interest that happened at a particular instance in time. Our goal is to capture the information needed in order to understand the event as it occurred. Here's a list of some information that would be useful to capture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A timestamp. Timestamps tell us when an event took place and when it occurred in relation to other events. Best practices suggest logging timestamps in a standard timezone, such as UTC, and using a standard format, such as ISO-8601.&lt;/li&gt;
&lt;li&gt;Context. We want to make it as clear as possible what the event is about. Trying to debug an issue using vague logged data or error names can be extremely frustrating. Providing a good description of the context of the error makes understanding and correcting it a lot more efficient.&lt;/li&gt;
&lt;li&gt;Log levels, such as &lt;strong&gt;Error&lt;/strong&gt;, &lt;strong&gt;Warning&lt;/strong&gt;, or &lt;strong&gt;Information&lt;/strong&gt;. These provide more context for understanding the issue and its severity. They allow us to prioritize and focus first on those issues that have the greatest impact. They also allow us to filter through our data and analyze it more effectively.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider this list as a baseline of information that would be helpful to log. Obviously, our simple application above falls short of these basic guidelines.&lt;/p&gt;

&lt;h2&gt;Issues With Client-Side Logging&lt;/h2&gt;

&lt;p&gt;Our simple demonstration of React logging may have provided us with some information about the application's behavior while running, but our logged information definitely wasn't persistent. If we were to refresh the browser or navigate to another page, the information would disappear.&lt;/p&gt;

&lt;p&gt;More importantly, our application was logging information to the console of the browser. If this application was released into production rather than just running locally, we would have no way to access those logged messages.&lt;/p&gt;

&lt;p&gt;With many of the technologies we've explored throughout this series, we were able to use the file system to satisfy the need for a persistent medium. However, as was the case when we looked at JavaScript, that approach won't work with React. There are several security issues involved in accessing the file system from the browser, and accessing local storage on unknown and inaccessible machines is not an option.&lt;/p&gt;

&lt;h2&gt;Extending Our Logging Application&lt;/h2&gt;

&lt;p&gt;Logging to the console has some benefit, mainly for debugging locally. But to get the full benefit of logging our data, we need something better. We need to find a way to satisfy the requirements outlined by the definition of logging given earlier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access to the logged information via the server and not the browser&lt;/li&gt;
&lt;li&gt;The ability to store our data using a persistent medium&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, we need a way to send messages to a server and write them to a persistent medium. Although this sounds simple, the work involved in creating a suitable mechanism is actually quite substantial. We would need to write a separate application with an available API to accept logging messages.&lt;/p&gt;

&lt;p&gt;Rather than bogging ourselves down with the numerous tasks involved in creating something from scratch, we can instead explore another option—namely a logging framework.&lt;/p&gt;

&lt;p&gt;A logging framework is a computer data logging package with methods allowing for logging at different levels. Fortunately for us, there are several existing options that satisfy our requirements.&lt;/p&gt;

&lt;h2&gt;Exploring a Logging Framework&lt;/h2&gt;

&lt;p&gt;For the purposes of this tutorial, we'll be looking at &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/universal-react-logger" rel="noopener noreferrer"&gt;universal-react-logger&lt;/a&gt;&lt;/strong&gt;. The description given states that it's&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;A logger to catch client errors on the server.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;In addition, events triggered in the browser are sent to the server. From there, they can be printed out immediately or sent to external log services for persistent storage. Both our requirements are satisfied!&lt;/p&gt;

&lt;p&gt;How does &lt;strong&gt;universal-react-logger&lt;/strong&gt; work? It takes advantage of error boundaries, which were introduced with React 16. Error boundaries are primarily used to catch errors during rendering but have been extended here to catch event errors as well.&lt;/p&gt;

&lt;h2&gt;Working With Universal React Logger&lt;/h2&gt;

&lt;p&gt;Now let's start implementing our logging framework. Once again, let's make sure we have all the requirements in order first.&lt;/p&gt;

&lt;h3&gt;Requirements and Installation&lt;/h3&gt;

&lt;p&gt;As you might expect, &lt;strong&gt;universal-react-logger&lt;/strong&gt; requires a React version of at least 16.2.0. To find out what version your project is running, find the &lt;strong&gt;node_modules&lt;/strong&gt; folder inside your &lt;strong&gt;my-app&lt;/strong&gt; application directory. Find &lt;strong&gt;/node_modules/react/package.json&lt;/strong&gt; and look for the &lt;strong&gt;version&lt;/strong&gt; key:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085923%2F06.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29085923%2F06.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your version of React is below 16.2.0, use the following command to upgrade:&lt;/p&gt;

&lt;pre&gt;npm update&lt;/pre&gt;

&lt;p&gt;Additionally, &lt;strong&gt;universal-react-logger&lt;/strong&gt; requires React Router DOM with a version of at least 4.2.2. Install it with this command:&lt;/p&gt;

&lt;pre&gt;npm install --save react-router-dom&lt;/pre&gt;

&lt;p&gt;Finally, install &lt;strong&gt;universal-react-logger&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;npm i universal-react-logger&lt;/pre&gt;

&lt;h3&gt;Create a New Component&lt;/h3&gt;

&lt;p&gt;Now that we're set up, we can create a new component and import the &lt;strong&gt;ErrorHandler&lt;/strong&gt; HOC to use with it. The example below is based on the code provided on the &lt;a href="https://www.npmjs.com/package/universal-react-logger" rel="noopener noreferrer"&gt;&lt;strong&gt;universal-react-logger&lt;/strong&gt; website&lt;/a&gt;. We'll start by creating a new file called &lt;strong&gt;Homepage.js&lt;/strong&gt; and importing &lt;strong&gt;ErrorHandler&lt;/strong&gt; from &lt;strong&gt;universal-react-logger&lt;/strong&gt;.&lt;/p&gt;

&lt;pre&gt;import { ErrorHandler} from 'universal-react-logger';&lt;/pre&gt;

&lt;p&gt;Next, we'll add a constructor to our &lt;strong&gt;Homepage&lt;/strong&gt; component to set up our state and bind a few new functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29090006%2F07.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29090006%2F07.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll also need to create the functions mentioned in the constructor, starting with the function that updates the counter:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29090035%2F08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29090035%2F08.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second function pretends to call a function that doesn't exist, resulting in an event error. It then sends the error using the &lt;strong&gt;setEventError&lt;/strong&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29090103%2F09.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flibrary.scalyr.com%2F2019%2F03%2F29090103%2F09.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, we'll update the render block. We'll add a condition to simulate a render error when the counter is equal to five, and we'll also have two buttons this time. One button will allow us to update the counter so we can eventually create a render error. The other will trigger an event error. The completed file should look like this:&lt;/p&gt;

&lt;pre&gt;import React, { Component } from 'react';
import { ErrorHandler} from 'universal-react-logger';

class Homepage extends Component {

    constructor(props) {
        super(props);
        this.state = {
            counter: 0,
            error: this.props.error,
            errorInfo: this.props.errorInfo
        };
        this.handleClick = this.handleClick.bind(this);
        this.makeError = this.makeError.bind(this);
    }

    handleClick() {
        this.setState(({counter}) =&amp;gt; ({
            counter: counter + 1
        }));
    }

    makeError () {
        try{
            // pretend to call a function that does not exist
            this.functionThatDontExist();
        } catch(error) {
            // send the error using the setEventError function
            this.props.setEventError(error);
        }
    };

    render() {
        if (this.state.counter === 5) {
            // Simulate a render error
            throw new Error('Error on render');
        }

        return (
            &amp;lt;div&amp;gt;
                &amp;lt;h1 key="welcome"&amp;gt;universal-react-logger&amp;lt;/h1&amp;gt;
                &amp;lt;button onClick={this.handleClick}&amp;gt;Update counter: {this.state.counter}&amp;lt;/button&amp;gt;
                &amp;lt;button onClick={() =&amp;gt; {this.makeError()}}&amp;gt;Make event error&amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
        );
    }
}

export default ErrorHandler(Homepage, true);&lt;/pre&gt;

&lt;h3&gt;Create a Route on the Server&lt;/h3&gt;

&lt;p&gt;With our component ready, we also need to add the corresponding route on the server. Inside the &lt;strong&gt;src&lt;/strong&gt; folder, you'll find the &lt;strong&gt;index.js&lt;/strong&gt; file. Open it and add the following:&lt;/p&gt;

&lt;pre&gt;/**
 * Post client errors in order to log them
 */
app.post('/log-client-errors', (req, res) =&amp;gt; {

    let error       = req.body.error.message;
    let errorInfo   = req.body.error.stack;

    // send these errors to some service or to a logger (ex: winston)
    //ex: logger.error(`The app received a new client log: ${error} ${errorInfo}`);

    res.status(200);
});&lt;/pre&gt;

&lt;p&gt;Now when we run the application with the server, we'll see our log messages printed to the console when we trigger an error. This means we've successfully sent a logging message from the browser back to the server!&lt;/p&gt;

&lt;h2&gt;What's Next?&lt;/h2&gt;

&lt;p&gt;Even with all the work we've done here, we've barely scratched the surface of logging in React. In particular, sending logging information from the client-side back to the server involves a whole new set of variables to consider. For example, browser information, URLs, and device information might be valuable information to transmit. The features and capabilities of different logging frameworks are also worth your consideration and should be influenced by an understanding of what information you need.&lt;/p&gt;

&lt;p&gt;No matter what you decide, you've undoubtedly recognized that client-side logging isn't enough. It can be helpful to get an impression of the overall behavior of your application, but to be really useful, you want to consider a way to get your logs back to the server.&lt;/p&gt;

&lt;p&gt;Knowing where to start with logging can be a daunting task, but having clear and detailed insight into the functioning of your software will be well worth the effort. Once you get to the point where you're collecting a considerable amount of data, you may want to organize it, search it, and represent it visually. You'd most likely benefit from matching client-side messages to server-side messages as well. If that's the case, your next step is to consider log aggregation, which happens to be Scalyr's specialty.&lt;/p&gt;

&lt;p&gt;To read more about the things you can do with your data, check out what we have to say about &lt;a href="https://www.scalyr.com/product" rel="noopener noreferrer"&gt;log aggregation&lt;/a&gt;! But in the meantime, start with a basic level of logging and build from there.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was written by &lt;/em&gt;&lt;a href="https://www.linkedin.com/in/delila-lee-92061552/" rel="noopener noreferrer"&gt;&lt;em&gt;Delila Lee&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. Delila s a software developer in the NLP and ML space with degrees in both linguistics and computer science. She currently works with functional programming languages, and she does some web development on occasion.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>logging</category>
      <category>programming</category>
      <category>coding</category>
      <category>react</category>
    </item>
  </channel>
</rss>
