<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>log4j Archives - Info Spot</title>
	<atom:link href="https://info-spot.net/tag/log4j/feed/" rel="self" type="application/rss+xml" />
	<link>https://info-spot.net/tag/log4j/</link>
	<description>Info Spot blog</description>
	<lastBuildDate>Tue, 03 Dec 2024 11:04:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>

<image>
	<url>https://info-spot.net/wp-content/uploads/2024/11/cropped-icon-32x32.png</url>
	<title>log4j Archives - Info Spot</title>
	<link>https://info-spot.net/tag/log4j/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Store app logs in AWS Cloudwatch using Log4J (or Logback) appender</title>
		<link>https://info-spot.net/logs-aws-cloudwatch-log4j-logback/</link>
					<comments>https://info-spot.net/logs-aws-cloudwatch-log4j-logback/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 03 Dec 2024 08:39:02 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[cloudwatch]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[logback]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=751</guid>

					<description><![CDATA[<p>One of the most important diagnostic tools available to us for locating problems with our apps is logging.&#8230;</p>
<p>The post <a href="https://info-spot.net/logs-aws-cloudwatch-log4j-logback/">Store app logs in AWS Cloudwatch using Log4J (or Logback) appender</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>One of the most important diagnostic tools available to us for locating problems with our apps is logging. Logging has historically involved writing text lines to a local filesystem file. This leads to a number of problems. In addition, it is now necessary to search through multiple backend servers to determine which one handled the request we were trying to find. Naturally, it should be noted that different backend instances may receive requests from the same session. The fact that any persistent data on an instance in a cloud environment expires along with the instance itself is even more serious. Furthermore, we no longer have active control over when certain instances are terminated in the case of auto-scaling. We must examine <strong>centralized logging</strong> as a novel answer for this. A central service receives all logs, aggregates them, stores them, and makes them searchable. For this, AWS provides a feature known as <strong>CloudWatch Logs</strong>.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img fetchpriority="high" decoding="async" width="1010" height="847" src="https://info-spot.net/wp-content/uploads/2024/12/image-4.png" alt="" class="wp-image-754" style="width:611px;height:auto" srcset="https://info-spot.net/wp-content/uploads/2024/12/image-4.png 1010w, https://info-spot.net/wp-content/uploads/2024/12/image-4-300x252.png 300w, https://info-spot.net/wp-content/uploads/2024/12/image-4-768x644.png 768w" sizes="(max-width: 1010px) 100vw, 1010px" /></figure></div>


<p>The images of those applications automatically reroute both <code>stdout </code>and <code>stderr </code>to the application&#8217;s CloudWatch Logs <strong>LogStream </strong>within the CloudWatch Logs <strong>LogGroup </strong>for the current environment using the open-source <strong>CloudCaptain </strong>CloudWatch Logs agent. </p>


<div class="wp-block-image">
<figure class="aligncenter"><img decoding="async" src="https://cloudcaptain.sh/assets/img/logback.jpg" alt=""/></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="363" height="139" src="https://info-spot.net/wp-content/uploads/2024/12/image-6.png" alt="" class="wp-image-760" srcset="https://info-spot.net/wp-content/uploads/2024/12/image-6.png 363w, https://info-spot.net/wp-content/uploads/2024/12/image-6-300x115.png 300w" sizes="(max-width: 363px) 100vw, 363px" /></figure></div>


<p>Providing a brand-new open-source appender for both <strong>Logback</strong> and <strong>Log4J</strong>, is one step further today for JVM apps. By natively integrating with the chosen logging framework and having the choice on how to route the logs, this provides a far more fine-grained control.</p>



<h2 class="wp-block-heading">Installation</h2>



<p>Simply include the dependency in your build file to incorporate the CloudCaptain Java log appender for AWS CloudWatch Logs into your application.</p>



<h3 class="wp-block-heading">Maven</h3>



<p>Start by adding the CloudCaptain Maven repository to your list of repositories in your&nbsp;<code>pom.xml</code>:</p>



<pre class="wp-block-code"><code>&lt;repositories&gt;
    &lt;repository&gt;
        &lt;id&gt;central&lt;/id&gt;
        &lt;url&gt;https://repo1.maven.org/maven2/&lt;/url&gt;
    &lt;/repository&gt;
    &lt;repository&gt;
        &lt;id&gt;boxfuse-repo&lt;/id&gt;
        &lt;url&gt;https://files.cloudcaptain.sh&lt;/url&gt;
    &lt;/repository&gt;
&lt;/repositories&gt;</code></pre>



<p>Then add the dependency:</p>



<pre class="wp-block-code"><code>&lt;dependency&gt;
    &lt;groupId&gt;com.boxfuse.cloudwatchlogs&lt;/groupId&gt;
    &lt;artifactId&gt;cloudwatchlogs-java-appender&lt;/artifactId&gt;
    &lt;version&gt;1.0.2.17&lt;/version&gt;
&lt;/dependency&gt;</code></pre>



<h3 class="wp-block-heading">Gradle</h3>



<p>Start by adding the <strong>CloudCaptain </strong>Maven repository to your list of repositories in your <code>build.gradle</code>:</p>



<pre class="wp-block-code"><code>repositories {
    mavenCentral()
    maven {
        url "https://files.cloudcaptain.sh"
    }
}</code></pre>



<p>Then add the dependency:</p>



<pre class="wp-block-code"><code>dependencies {
    compile 'com.boxfuse.cloudwatchlogs:cloudwatchlogs-java-appender:1.0.2.17'
}</code></pre>



<h2 class="wp-block-heading">Usage</h2>



<p>To use the appender you must add it to the configuration of your logging system.</p>



<h3 class="wp-block-heading">Logback</h3>



<p>Add the appender to your&nbsp;<code>logback.xml</code>&nbsp;file at the root of your classpath. In a Maven or Gradle project you can find it under&nbsp;<code>src/main/resources</code>:</p>



<pre class="wp-block-code"><code>&lt;configuration&gt;
    &lt;appender name="CloudCaptain-CloudwatchLogs" class="com.boxfuse.cloudwatchlogs.logback.CloudwatchLogsLogbackAppender"/&gt;

    &lt;root level="debug"&gt;
        &lt;appender-ref ref="CloudCaptain-CloudwatchLogs" /&gt;
    &lt;/root&gt;
&lt;/configuration&gt;</code></pre>



<h3 class="wp-block-heading">Log4J2</h3>



<p>Add the appender to your&nbsp;<code>log4j2.xml</code>&nbsp;file at the root of your classpath. In a Maven or Gradle project you can find it under&nbsp;<code>src/main/resources</code>:</p>



<pre class="wp-block-code"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;Configuration packages="com.boxfuse.cloudwatchlogs.log4j2"&gt;
    &lt;Appenders&gt;
        &lt;CloudCaptain-CloudwatchLogs/&gt;
    &lt;/Appenders&gt;
    &lt;Loggers&gt;
        &lt;Root level="debug"&gt;
            &lt;AppenderRef ref="CloudCaptain-CloudwatchLogs"/&gt;
        &lt;/Root&gt;
    &lt;/Loggers&gt;
&lt;/Configuration&gt;</code></pre>



<h3 class="wp-block-heading">Code</h3>



<p>And that&#8217;s all the setup you need! With <strong>SLF4J </strong>or your preferred api, you can now begin using it from code as you typically would.</p>



<p>Logging a message like this:</p>



<pre class="wp-block-code"><code>private static final Logger LOGGER = LoggerFactory.getLogger(MyClazz.class);
...
LOGGER.info("This is a log message ...");</code></pre>



<p>will now be automatically sent as a structured <strong>JSON </strong>document to <strong>CloudWatch </strong>Logs. Important metadata is also automatically added to the document by the <strong>CloudCaptain </strong>CloudWatch Logs appender, making it appear as follows:</p>



<pre class="wp-block-code"><code>{
    "image": "myuser/myapp:123",
    "instance": "i-607b5ddc",
    "level": "INFO",
    "logger": "org.mycompany.myapp.MyClazz",
    "message": "This is a log message ...",
    "thread": "main"
}</code></pre>



<p>This is very useful as this will allow us to query and filter the logs later.</p>



<p>Keep in mind that these are only the automatically sent properties. you can also utilize the MDC to fill in a lot of additional ones (such current user, session id, request id, etc.).</p>



<h2 class="wp-block-heading">Displaying the Logs</h2>



<p>To display the logs simply open a new terminal and show the logs for your app in your desired environment:</p>



<pre class="wp-block-preformatted">&gt; boxfuse logs myapp -env=prod</pre>



<h3 class="wp-block-heading">Live tailing</h3>



<p>And if you want to follow along in real time you can use log tailing:</p>



<pre class="wp-block-code"><code>&gt; boxfuse logs myapp -env=prod <strong>-logs.tail</strong></code></pre>



<p>And new logs will now automatically be displayed as soon as they are sent from the application to CloudWatch Logs.</p>



<h3 class="wp-block-heading">Log filtering</h3>



<p>But this can yield a lot of results. CloudCaptain also offers robust log filtering to help discover the needle in the haystack, both on old logs and when a log stream is being tailed live.</p>



<p>The structured logs&#8217; properties can be used to precisely filter them as you see fit. For instance, you can only display the logs for a particular instance and tail the logs live on the production environment on AWS by doing the following:</p>



<pre class="wp-block-code"><code>&gt; boxfuse logs myapp -env=prod -logs.tail <strong>-logs.filter.instance=</strong>i-607b5ddc</code></pre>



<p>And if you aren&#8217;t quite sure what you are looking for you can also simply filter by time. For example to show all the logs created in the last minute (60 seconds) you could do:</p>



<pre class="wp-block-code"><code>&gt; boxfuse logs myapp -env=prod -logs.tail <strong>-logs.filter.start=</strong>-60</code></pre>



<p>source: <a href="https://cloudcaptain.sh/blog/logback-log4j2-appender">https://cloudcaptain.sh/blog/logback-log4j2-appender</a></p>
<p>The post <a href="https://info-spot.net/logs-aws-cloudwatch-log4j-logback/">Store app logs in AWS Cloudwatch using Log4J (or Logback) appender</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/logs-aws-cloudwatch-log4j-logback/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
