<?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>Programming Archives - Info Spot</title>
	<atom:link href="https://info-spot.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://info-spot.net/category/programming/</link>
	<description>Info Spot blog</description>
	<lastBuildDate>Thu, 05 Dec 2024 11:40:28 +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>Programming Archives - Info Spot</title>
	<link>https://info-spot.net/category/programming/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to remove a property from an object in Javascript</title>
		<link>https://info-spot.net/how-to-remove-a-property-from-an-object-in-javascript/</link>
					<comments>https://info-spot.net/how-to-remove-a-property-from-an-object-in-javascript/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 05 Dec 2024 11:27:46 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[delete property from JS object]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[remove property from javascript object]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=829</guid>

					<description><![CDATA[<p>Working with objects is an essential component of creating apps with JavaScript. Whether it&#8217;s to clean up data&#8230;</p>
<p>The post <a href="https://info-spot.net/how-to-remove-a-property-from-an-object-in-javascript/">How to remove a property from an object in Javascript</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Working with objects is an essential component of creating apps with JavaScript. Whether it&#8217;s to clean up data or get an object ready for a particular use case, you may frequently need to delete properties from an object. This article examines several methods for deleting properties from a JavaScript object, going over each one&#8217;s benefits and drawbacks. We&#8217;ll also go over how to effectively remove several properties from a single object.</p>



<h2 class="wp-block-heading" id="methods-for-removing-properties-from-a-javascript-object">1. Using the <code>delete</code> Operator</h2>



<p>The delete operator is the simplest method for deleting a property from a JavaScript object. Although this operator is simple and easy to use, developers should be aware of some of its limitations.</p>



<pre class="wp-block-code"><code>const employee= {
  name: 'John Doe',
  age: 33,
  job: 'Manager'
}

delete employee.age

console.log(employee) // Output: { name: 'John Doe', job: 'Manager' }</code></pre>



<p>A JavaScript object&#8217;s given property can be deleted using the delete operator. The property can no longer be accessed within the original object after deletion. However, if a property is present in the prototype, it can still be accessed because the delete operator has no effect on the object&#8217;s prototype chain.</p>



<p>While the <code>delete</code> operator is easy to use, it has a few downsides:</p>



<ul class="wp-block-list">
<li>Performance Problems: In applications where performance is crucial, the delete operator may result in a decline in performance. JavaScript engines optimize objects for speed; nevertheless, de-optimization may occur if a property is removed.</li>



<li>Mutating Objects: By eliminating the designated property, the delete operator modifies the original object. Unexpected side effects may result from this, particularly when working with shared objects in a global scope.</li>
</ul>



<h3 class="wp-block-heading" id="2-setting-the-property-to-undefined-or-null">2. Setting the Property to&nbsp;<code>undefined</code>&nbsp;or&nbsp;<code>null</code></h3>



<p>Setting a property&#8217;s value to undefined or null is an additional method for eliminating properties from a JavaScript object. This shows that the property has no value but does not physically delete it from the object.</p>



<pre class="wp-block-code"><code>const employee= {
  name: 'John Doe',
  age: 33,
  job: 'Manager'
}

employee.age = undefined

console.log(employee) // Output: { name: 'John Doe',  age: undefined, job: 'Manager' }</code></pre>



<p><strong>Differences Between&nbsp;<code>undefined</code>&nbsp;and&nbsp;<code>null</code></strong></p>



<ul class="wp-block-list">
<li><code>Undefined</code>: Indicates a property or variable that has not yet been given a value.</li>



<li><code>null</code>: Indicates that there is no object value present on purpose.</li>
</ul>



<p>When you wish to communicate that a property has no value but yet need to preserve the original object&#8217;s structure, this method can be helpful. When a property is set to null or undefined, it indicates that the property is still present in the object. If other sections of your code require the property to be totally absent, this could cause confusion.</p>
<p>The post <a href="https://info-spot.net/how-to-remove-a-property-from-an-object-in-javascript/">How to remove a property from an object in Javascript</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/how-to-remove-a-property-from-an-object-in-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Is Java &#8220;pass-by-reference&#8221; or &#8220;pass-by-value&#8221;?</title>
		<link>https://info-spot.net/is-java-pass-by-reference-or-pass-by-value/</link>
					<comments>https://info-spot.net/is-java-pass-by-reference-or-pass-by-value/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 04 Dec 2024 07:25:50 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java programming]]></category>
		<category><![CDATA[pass by reference]]></category>
		<category><![CDATA[pass by value]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=801</guid>

					<description><![CDATA[<p>A lot of Java programmers wonder if Java is passable by reference or by value. The reason Java&#8230;</p>
<p>The post <a href="https://info-spot.net/is-java-pass-by-reference-or-pass-by-value/">Is Java &#8220;pass-by-reference&#8221; or &#8220;pass-by-value&#8221;?</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A lot of Java programmers wonder if Java is passable by reference or by value. The reason Java is always pass by value is summed up in this article. First, what are the meanings of pass by reference and pass by value?</p>



<ul class="wp-block-list">
<li>Pass by value: The copied object is supplied to the method after the method parameter values have been transferred to another variable. The copy is used in the technique.</li>
</ul>



<ul class="wp-block-list">
<li>Pass by reference: The method receives an alias or reference to the real parameter. The actual parameter is accessed by the method.</li>
</ul>



<p>The idea of the object reference in Java is frequently the cause of the misunderstanding surrounding these concepts. Although a variable may contain a reference to an object, the object reference is actually a value that indicates the object&#8217;s position in memory, therefore technically, Java is always pass by value. As a result, object references are supplied by value.</p>



<p>Primitive and reference data types are both provided by value. Study up on Java&#8217;s data types. Because reference data types and basic data types are stored differently, it&#8217;s crucial to comprehend memory allocation in Java in addition to data types.</p>



<h3 class="wp-block-heading" id="demonstrating-pass-by-value">pass by value Example<a href="https://www.digitalocean.com/community/tutorials/java-is-pass-by-value-and-not-pass-by-reference#demonstrating-pass-by-value"></a></h3>



<p>The following example demonstrates how values are passed in Java. </p>



<pre class="wp-block-code"><code>public class Car{
	private String color;
	public Car() {}	
	public Car(String c) {
		this.color = c;
	}	
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
}</code></pre>



<p>The following example program uses a generic method, <code>swap()</code>, that swaps two variables. Another method, <code>changeValues()</code>, attempts to change the variable values.</p>



<pre class="wp-block-code"><code>public class Test {
	public static void main(String&#91;] args) {
		Car red = new Car("Red"); // memory reference = 50
		Car blue = new Car("Blue"); // memory reference = 100
		
		swap(red, blue);
		System.out.println("After the swap method executes:");
		System.out.println("`red` color value = " + red.getColor());
		System.out.println("`blue` color value = " + blue.getColor());
		
		changeValues(blue);
		System.out.println("After the changeValues method executes:");
		System.out.println("`blue` color value = " + blue.getColor());		
	}
	// Generic swap method
	public static void swap(Object o1, Object o2){
		Object temp = o1;
		o1 = o2;
		o2 = temp;
	}
	private static void changeValues(Car car) { // car= 100
		car.setColor("Red"); // car= 100
		car = new Car("Green"); // car = 200
		car.setColor("Blue"); // car = 200
	}
}</code></pre>



<p>When you execute the example program, you get the following output:</p>



<pre class="wp-block-code"><code>OutputAfter the swap method executes:
'red' color value = Red
'blue' color value = Blue
After the changeValues method executes:
'blue' color value = Red</code></pre>



<p>The result demonstrates that the color values of the original objects were not changed by the <code>swap</code>() method. Because the <code>swap</code>() function only works with copies of the original object reference values, it helps demonstrate that Java is pass by value. Any programming language can be used to determine whether a method is pass by reference or pass by value using the <code>swap</code>() method test.</p>



<h3 class="wp-block-heading" id="the-example-swap-method-explained"><code>swap()</code> Method Explained<a href="https://www.digitalocean.com/community/tutorials/java-is-pass-by-value-and-not-pass-by-reference#the-example-swap-method-explained"></a></h3>



<p>When you use the&nbsp;<code>new</code>&nbsp;operator to create an instance of a class, the object is created and the variable contains the location in memory where the object is saved.</p>



<pre class="wp-block-code"><code>Car red = new Car("Red");
Car blue = new Car("Blue");</code></pre>



<p>Here’s a step-by-step breakdown of what happens when the&nbsp;<code>swap()</code>&nbsp;method executes:</p>



<ul class="wp-block-list">
<li>Suppose that both car objects&#8217; memory addresses are 50 and 100, respectively, and that red and blue are pointing to these locations.</li>



<li>Two new object variables, o1 and o2, are produced when the class invokes the swap() function with the red and blue variables as parameters.Additionally, memory locations 50 and 100 are indicated by o1 and o2, respectively.</li>



<li>The snippet of code that follows describes what occurs in the swap() method:swap(Object o1, Object o2) and public static void // o2 = 100, o1 = 50 Assign the object reference value of o1 to temp by using the formula temp = 50, o1 = 50, o2 = 100. temp = 50, o1 = 100, o2 = 100 o1 = o2; // assign the object reference value of o2 to o1: o2 = temp; // assign the object</li>
</ul>



<p>Java is pass by reference, so it&#8217;s a common misconception to think that you&#8217;re passing the reference because the variables hold the reference to the objects. It is pass-by value, though, because you are providing a value that is a copy of the reference.</p>



<h3 class="wp-block-heading" id="the-example-changevalue-method-explained"><code>changeValues()</code> Method Explained<a href="https://www.digitalocean.com/community/tutorials/java-is-pass-by-value-and-not-pass-by-reference#the-example-changevalue-method-explained"></a></h3>



<p>The next method in the example program changes the color value of the object referenced by the&nbsp;<code>blue</code>&nbsp;variable:</p>



<pre class="wp-block-code"><code>private static void changeValues(Car car) { // car = 100
	car.setColor("Red"); // car = 100
	car = new Car("Green"); // car = 200
	car.setColor("Blue"); // car = 200
}</code></pre>



<p>Here’s a step-by-step breakdown of what happens within the <code>changeValues()</code> method:</p>



<ul class="wp-block-list">
<li>The blue variable that points to memory address 100 is called by the class using the <code>changeValues</code>() method. A reference that also points to memory address 100 is created in the first line. The item at memory address 100 now has the color value &#8220;Red&#8221; instead.</li>



<li>The new object (with color value &#8220;Green&#8221;) is created in the second line. Memory location 200 is where the new object is located. The object at memory address 200 is affected by any additional operations performed on the car variable, while the object at memory location 100 is unaffected. The reference made in line 1 is overwritten by the new car variable, and this method no longer makes the car reference available.</li>



<li>The old object that blue references at memory address 100 remains unaltered, but the third line adds &#8220;Blue&#8221; as the color value of the new car object at memory location 200. This explains why the example program output&#8217;s last line, which represents the change from line 1, prints blue color value = Red.</li>
</ul>
<p>The post <a href="https://info-spot.net/is-java-pass-by-reference-or-pass-by-value/">Is Java &#8220;pass-by-reference&#8221; or &#8220;pass-by-value&#8221;?</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/is-java-pass-by-reference-or-pass-by-value/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Backup and Restore of Docker Volumes</title>
		<link>https://info-spot.net/backup-restore-docker-volumes/</link>
					<comments>https://info-spot.net/backup-restore-docker-volumes/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 03 Dec 2024 09:24:55 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[docker backup]]></category>
		<category><![CDATA[docker restore]]></category>
		<category><![CDATA[docker volumes]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=768</guid>

					<description><![CDATA[<p>Knowing how to backup and restore a volume in Docker is becoming more and more crucial as more&#8230;</p>
<p>The post <a href="https://info-spot.net/backup-restore-docker-volumes/">Backup and Restore of Docker Volumes</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Knowing how to backup and restore a volume in Docker is becoming more and more crucial as more developers use it to expedite their development processes. This article will discuss the problems associated with volume backup and restoration, the rationale behind them, and offer a complete solution that will make the process simple for you.<br><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-what" target="_blank" rel="noreferrer noopener"></a></p>



<p>Because it makes it easy to construct and manage containerized apps, Docker has become a vital tool for many developers in recent years. One of Docker&#8217;s biggest benefits is its capacity to control data volumes, which lets you store data that lasts longer than a container. Docker&#8217;s volume control can occasionally be difficult, despite its many advantages. Specifically, creating backups and recovering volumes can be difficult and prone to mistakes. We&#8217;ll look at why you might need to backup your volumes in this post, as well as how to do it properly. <a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-why" target="_blank" rel="noreferrer noopener"></a>There are many compelling reasons to do so. Here are a few reasons why you might need to take backups of your Docker volumes:</p>



<h3 class="wp-block-heading" id="-1"><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-disaster-recovery" target="_blank" rel="noreferrer noopener"></a><strong>Disaster Recovery</strong></h3>



<p>The most evident justification for backing up your Docker volumes is to guard against data loss in the case of an emergency. You can make sure that your important data is secure and recoverable in an emergency by regularly backing up your volumes.</p>



<h3 class="wp-block-heading" id="-2"><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-testing-and-development" target="_blank" rel="noreferrer noopener"></a><strong>Testing and Development</strong></h3>



<p>Making backups of your Docker volumes is also necessary for testing and development. You may quickly revert to a previous state in case of issues or the need to test new features by making backups of your volumes at various points during the development process.</p>



<h3 class="wp-block-heading" id="-3"><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-replication" target="_blank" rel="noreferrer noopener"></a><strong>Replication</strong></h3>



<p>Replicating data across different environments might also benefit from backing up your Docker volumes. You may guarantee that your data is consistent throughout all of your development, staging, and production environments by making backups of your volumes and restoring them in different settings.</p>



<h2 class="wp-block-heading" id="-4"><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-how" target="_blank" rel="noreferrer noopener"></a><strong>Detailed steps</strong></h2>



<p>After discussing the issues and justifications for backups and volume restoration in Docker, let&#8217;s see how to accomplish it properly. The following procedures should be followed in order to create backups of your volumes:</p>



<h3 class="wp-block-heading" id="-5"><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-step-1-identify-the-volume" target="_blank" rel="noreferrer noopener"></a><strong>Step 1: Identify the Volume</strong></h3>



<p>Selecting the volume you wish to backup is the first step in creating a copy of that volume. This can be accomplished by executing the subsequent command:</p>



<pre class="wp-block-code"><code>docker volume ls</code></pre>



<p>This will show you a list of all the volumes that are currently available on your Docker host. Note down the name of the volume that you want to back up.</p>



<h3 class="wp-block-heading" id="-6"><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-step-2-create-a-backup" target="_blank" rel="noreferrer noopener"></a><strong>Step 2: Create a Backup</strong></h3>



<p>To create a backup of the volume, you can use the&nbsp;<code>docker run</code>&nbsp;command to start a container that mounts the volume you want to back up and a separate container that writes the backup data to a file.</p>



<p>Here’s an example of how to do this:</p>



<pre class="wp-block-code"><code>docker run --rm \
--mount source=&lt;volume-name&gt;,target=&lt;target&gt; \
-v $(pwd):/backup \
busybox \
tar -czvf /backup/&lt;backup-filename&gt;.tar.gz &lt;target&gt;</code></pre>



<p>In this command, replace&nbsp;<code>&lt;volume-name&gt;</code>&nbsp;with the name of the volume you want to back up,&nbsp;<code>&lt;target&gt;</code>&nbsp;with the mount point inside the docker container, and&nbsp;<code>&lt;backup-filename&gt;</code>&nbsp;with a name for the backup file.</p>



<h3 class="wp-block-heading" id="-7"><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-step-3-move-the-backup-file-to-an-external-server" target="_blank" rel="noreferrer noopener"></a><strong>Step 3: Move the Backup File to an External Server</strong></h3>



<p>To make sure your backup file is safe and secure, it&#8217;s a good idea to relocate it to an external server or storage device after creating it. In the event of a calamity, such as a server failure or security breach, the backup file can be better protected by being stored on a different server or storage device. <strong>SCP </strong>can be used to transfer the backup file to an external server.</p>



<p>Using <strong>SSH</strong>, you can move files between servers using Secure Copy (SCP), a secure file transfer protocol. You must have SSH access to the source and destination servers in order to utilize SCP. The backup file can be copied to the external server using the following command:</p>



<pre class="wp-block-code"><code>scp /path/to/backupfile user@external-server:/path/to/destination</code></pre>



<h3 class="wp-block-heading" id="-8"><a href="https://headingtag.com/how-to-take-backup-and-restore-a-volume-in-docker#heading-step-4-restore-the-volume" target="_blank" rel="noreferrer noopener"></a><strong>Step 4: Restore the Volume</strong></h3>



<p>If you need to restore the volume from the backup, you can use the&nbsp;<code>docker run</code>&nbsp;command to start a container that mounts the backup file and a separate container that writes the backup data to the volume.</p>



<p>Here’s an example of how to do this:</p>



<pre class="wp-block-code"><code>docker run --rm \
--mount source=&lt;volume-name&gt;,target=&lt;target&gt; \
-v $(pwd):/backup \
busybox \
tar -xzvf /backup/&lt;backup-filename&gt;.tar.gz -C /</code></pre>



<p>In this command, replace&nbsp;<code>&lt;volume-name&gt;</code>&nbsp;with the name of the volume you want to back up,&nbsp;<code>&lt;target&gt;</code>&nbsp;with the mount point inside the docker container, and&nbsp;<code>&lt;backup-filename&gt;</code>&nbsp;with a name for the backup file.</p>



<p></p>
<p>The post <a href="https://info-spot.net/backup-restore-docker-volumes/">Backup and Restore of Docker Volumes</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/backup-restore-docker-volumes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
		<item>
		<title>Git: Force `git pull` to overwrite local files</title>
		<link>https://info-spot.net/force-git-pull-to-overwrite-local-files/</link>
					<comments>https://info-spot.net/force-git-pull-to-overwrite-local-files/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 29 Nov 2024 16:36:53 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[force git]]></category>
		<category><![CDATA[git pull]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=690</guid>

					<description><![CDATA[<p>When you run git pull, does the command fail with an error message indicating that local files will be&#8230;</p>
<p>The post <a href="https://info-spot.net/force-git-pull-to-overwrite-local-files/">Git: Force `git pull` to overwrite local files</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>When you run <code>git pull</code>, does the command fail with an error message indicating that local files will be overwritten? If so  then let&#8217;s see how to force <code>git pull</code> to execute anyway?</p>



<p>Using <code>git stash</code> is the most straightforward and secure method for accomplishing this. This command will restore a repository to the state of its most recent commit and save any modifications made to its working directory since then. To retrieve remote changes, you can then use the <code>git pull</code> command. In order to reapply the uncommitted changes on top of the new commits, run <code>git stash pop</code>.</p>
<p>The post <a href="https://info-spot.net/force-git-pull-to-overwrite-local-files/">Git: Force `git pull` to overwrite local files</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/force-git-pull-to-overwrite-local-files/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JavaScript: How Can I Remove a Specific Item from an Array?</title>
		<link>https://info-spot.net/how-can-i-remove-a-specific-item-from-an-array/</link>
					<comments>https://info-spot.net/how-can-i-remove-a-specific-item-from-an-array/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 29 Nov 2024 08:40:27 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[js arrays]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=659</guid>

					<description><![CDATA[<p>In JavaScript, there are several ways to exclude particular entries from an array. You can choose a method&#8230;</p>
<p>The post <a href="https://info-spot.net/how-can-i-remove-a-specific-item-from-an-array/">JavaScript: How Can I Remove a Specific Item from an Array?</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In JavaScript, there are several ways to exclude particular entries from an array. You can choose a method based on whether you&#8217;ll identify the item by its value or its index if the <code>pop()</code> or <code>shift()</code> methods aren&#8217;t suitable for your needs.</p>



<h3 class="wp-block-heading" id="removing-the-last-element-of-an-array">Removing the Last Element of an Array</h3>



<p>The&nbsp;<code>pop()</code>&nbsp;method removes and returns the last element of an array.</p>



<pre class="wp-block-code"><code>const myArray = &#91;1, 2, 3, 4, 5];

const x = myArray.pop();

console.log(`The values of myArray are: ${myArray}`);
console.log(`The element x value is: ${x}`);</code></pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>The values of myArray are: 1,2,3,4
The element x value is: 5</code></pre>



<h3 class="wp-block-heading" id="removing-the-first-element-of-an-array">Removing the First Element of an Array</h3>



<p>The&nbsp;<code>shift()</code>&nbsp;method removes and returns the first element of an array.</p>



<pre class="wp-block-code"><code>const myArray = &#91;1, 2, 3, 4, 5];

const x = myArray.shift();

console.log(`The values of myArray are: ${myArray}`);
console.log(`The element x value is: ${x}`);</code></pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>The values of myArray are: 2,3,4,5
The element x value is: 1</code></pre>



<h3 class="wp-block-heading" id="removing-an-element-by-index">Removing an Element by Index</h3>



<p>If you are identifying the item to be removed by its index, you can use the delete operator. If you want to use the value of the item you are removing, use the <code>splice()</code> method.</p>



<h4 class="wp-block-heading" id="the-delete-operator">The&nbsp;<code>delete</code>&nbsp;Operator</h4>



<p>The value at that position of the array will be <code>undefined </code>since the <code>delete </code>operator removes the object property at the designated index without changing the array&#8217;s length.</p>



<pre class="wp-block-code"><code>const myArray = &#91;1, 2, 3, 4, 5];

delete myArray&#91;1];

console.log(`The values of myArray are: ${myArray}`);</code></pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>The values of myArray are: 1,,3,4,5</code></pre>



<h4 class="wp-block-heading" id="the-splice-method">The&nbsp;<code>splice()</code>&nbsp;Method</h4>



<p>The index of the element you want to remove and the index you want to remove up to are the two arguments that the <code>splice() </code>function requires. All of the values that were deleted from the original array are stored in a new array created by the <code>splice() </code>method. The length of the original array will be adjusted, and the values that were eliminated will no longer be present.</p>



<pre class="wp-block-code"><code>const myArray = &#91;1, 2, 3, 4, 5];

const x = myArray.splice(1, 1);

console.log(`The values of myArray are: ${myArray}`);
console.log(`The element x value is: ${x}`);</code></pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>The values of myArray are: 1,3,4,5
variable x value: 2</code></pre>



<h3 class="wp-block-heading" id="removing-an-element-by-value">Removing an Element by Value</h3>



<p>Once the index has been identified using the <code>indexOf</code>() method, you can delete the element from its index if you are identifying the element to be removed by its value. Use the <code>filter</code>() technique or a combination of the <code>indexOf</code>() and <code>splice</code>() methods if you wish to use the value of the element you are deleting.</p>



<h4 class="wp-block-heading" id="combining-indexof-and-splice-methods">Combining&nbsp;<code>indexOf()</code>&nbsp;and&nbsp;<code>splice()</code>&nbsp;Methods</h4>



<p>The <code>indexOf</code>() method returns the index of the element in the array that corresponds to the value you pass in for the element you want to delete from your array. The element at the returned index is then removed using the <code>splice</code>() technique.</p>



<pre class="wp-block-code"><code>const myArray = &#91;1, 2, 3, 4, 5];

const index = myArray.indexOf(2);

const x = myArray.splice(index, 1);

console.log(`The values of myArray are: ${myArray}`);
console.log(`The element x value is: ${x}`);</code></pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>The values of myArray are: 1,3,4,5
The element x value is: 2</code></pre>
<p>The post <a href="https://info-spot.net/how-can-i-remove-a-specific-item-from-an-array/">JavaScript: How Can I Remove a Specific Item from an Array?</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/how-can-i-remove-a-specific-item-from-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Git: Undo the most recent local Git commits</title>
		<link>https://info-spot.net/git-undo-the-most-recent-local-git-commits/</link>
					<comments>https://info-spot.net/git-undo-the-most-recent-local-git-commits/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 29 Nov 2024 07:37:53 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[git reflog]]></category>
		<category><![CDATA[git reset]]></category>
		<category><![CDATA[git undo changes]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=634</guid>

					<description><![CDATA[<p>The current branch will be restored to a specified prior commit using the git reset command. By default,&#8230;</p>
<p>The post <a href="https://info-spot.net/git-undo-the-most-recent-local-git-commits/">Git: Undo the most recent local Git commits</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>The current branch will be restored to a specified prior commit using the <code>git reset</code> command. By default, this command will preserve the files in the working tree while deleting commits from the history of the current branch. By doing this, you can undo one or more commits without erasing any of your work.</p>



<p>You must indicate the commit you want to reset to when you call <code>git reset</code>. You can use the <code>tilde (~) </code>suffix to identify an ancestor of <code>HEAD</code>, the current commit, or you can use the <code>git log</code> to obtain the hash of the desired commit. The most recent commit can be undone and redone using the commands below:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
git reset HEAD~
git add main.py <em># need to re-add files after reset</em>
git commit -m "This commit corrects the mistake"</code></pre>



<p>To undo the last two commits, use the commands:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
<em># make changes</em>
git add .
git commit -m "This commit is another mistake"
git reset HEAD~2
git add .
git commit -m "this commit corrects both mistakes"</code></pre>



<p>If you don’t want to have to re-stage your files after a reset, you can use the&nbsp;<code>--soft</code>&nbsp;flag:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
git reset --soft HEAD~
<em># no need to git add, as files are already staged</em>
git commit -m "This commit corrects the mistake"</code></pre>



<p>The <code>--hard</code> flag can be used to restore the working tree and Git history to the state of a prior commit. Keep in mind that doing this would undo all tracked file modifications, even those that haven&#8217;t been committed yet.</p>



<pre class="wp-block-code"><code>git add .
git commit -m ""
git reset --hard HEAD~</code></pre>



<p>Use of <code>git reset --hard</code> should be done carefully. Nevertheless, up to 90 days after they were erased, you can still use <code>git reflog</code> to recover any deleted changes. <code>Git reflog</code> will display a list of prior commits on the tips of branches when it is executed. You can select the partial hash of the commit (such as <code>5c8f5a7</code>) from this list in order to restore it and make a new branch for it:</p>



<pre class="wp-block-code"><code>git checkout -b restored-commit-branch 5c8f5a7</code></pre>



<p><code>Git revert</code> can be used to produce new commits that do the opposite of current commits, i.e., removing lines and files that were added and adding lines and files that were removed, if you want to keep your repository&#8217;s history intact but restore the contents to a former state:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
git revert HEAD <em># will create a new commit doing the opposite of the one above</em></code></pre>
<p>The post <a href="https://info-spot.net/git-undo-the-most-recent-local-git-commits/">Git: Undo the most recent local Git commits</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/git-undo-the-most-recent-local-git-commits/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Git: Revert a Git repository to a previous commit</title>
		<link>https://info-spot.net/git-revert-a-git-repository-to-a-previous-commit/</link>
					<comments>https://info-spot.net/git-revert-a-git-repository-to-a-previous-commit/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 29 Nov 2024 07:15:27 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=625</guid>

					<description><![CDATA[<p>The word &#8220;revert&#8221; has a unique meaning in Git. Without changing the commit history, you can restore the&#8230;</p>
<p>The post <a href="https://info-spot.net/git-revert-a-git-repository-to-a-previous-commit/">Git: Revert a Git repository to a previous commit</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>The word &#8220;revert&#8221; has a unique meaning in Git. Without changing the commit history, you can restore the files in your repository to a former state by using the <code>git revert</code> command. To do this, fresh commits are made that perform the opposite action of previous commits, i.e., adding files and lines that were removed and deleting lines and files that were added.</p>



<p>To revert the most recently created commit, you can specify its hash or use&nbsp;<code>HEAD</code>:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
git revert HEAD <em># will create a new commit doing the opposite of the one above</em></code></pre>



<p>You can choose a range, from oldest to newest, to undo several recent changes. For every commit that is retracted, a new commit will be made.</p>



<pre class="wp-block-code"><code>git revert HEAD~2...HEAD <em># revert the last two commits</em></code></pre>



<p>Restoring a prior state while keeping the repository&#8217;s edit history is possible with <code>git revert</code>. But sometimes you might want to remove past commitments instead of undoing them. <code>Git reset --hard</code>, with the commit to return to specified, can be used to accomplish this:</p>



<pre class="wp-block-code"><code>git reset --hard HEAD~</code></pre>



<p>This will return the repository’s files to their previous state and remove the most recent commit from the current branch’s history.</p>
<p>The post <a href="https://info-spot.net/git-revert-a-git-repository-to-a-previous-commit/">Git: Revert a Git repository to a previous commit</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/git-revert-a-git-repository-to-a-previous-commit/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Redirect to Another Webpage using JavaScript?</title>
		<link>https://info-spot.net/how-to-redirect-to-another-webpage-using-javascript/</link>
					<comments>https://info-spot.net/how-to-redirect-to-another-webpage-using-javascript/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 29 Nov 2024 07:01:50 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[js redirect]]></category>
		<category><![CDATA[location href]]></category>
		<category><![CDATA[location replace]]></category>
		<category><![CDATA[redirect url]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=621</guid>

					<description><![CDATA[<p>JavaScript can redirect the users from current page to another webpage (different URL) with and without requiring any&#8230;</p>
<p>The post <a href="https://info-spot.net/how-to-redirect-to-another-webpage-using-javascript/">How to Redirect to Another Webpage using JavaScript?</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>JavaScript can redirect the users from current page to another webpage (different URL) with and without requiring any manual action (like clicking a link). This can be achieved in JavaScript using the <strong>window.location</strong> object and it will allow us to redirect programmatically.</p>



<h4 class="wp-block-heading" id="using-locationhref-to-redirect-to-another-webpage">Using window.<strong>location.href</strong></h4>



<p>The window location <strong>href </strong>property is used to set or return the full URL of the current page. The Location href property can also be used to set the href value redirect to another link. The Location href property returns a string that contains the entire URL of the page, including the protocol (http, https, etc). </p>



<p><strong>Syntax</strong></p>



<pre class="wp-block-code"><code>window.location.href = "URL"</code></pre>



<h4 class="wp-block-heading" id="using-locationreplace-to-redirect-to-another-webpage">Using&nbsp;<strong>location.replace() Method</strong></h4>



<p>The l<strong>ocation replace() </strong>method<a href="https://www.geeksforgeeks.org/html-dom-location-replace-method/" target="_blank" rel="noreferrer noopener"> </a>is used to replace the current document with the specified one. This method is different from the assign() method as this removes the current document from the document history, therefore it is not possible to go back to the previous document using the ‘back’ button. </p>



<p><strong>Syntax</strong></p>



<pre class="wp-block-code"><code>location.replace("URL");</code></pre>



<h4 class="wp-block-heading" id="3-how-to-redirect-browser-window-back-using-javascript-">How to redirect browser window back using JavaScript ?</h4>



<p>There are two approaches used to redirect the browser window back:</p>



<ul class="wp-block-list">
<li>Using <strong>history.back()</strong> Method</li>



<li>Using <strong>history.go()</strong> Method</li>
</ul>



<p></p>
<p>The post <a href="https://info-spot.net/how-to-redirect-to-another-webpage-using-javascript/">How to Redirect to Another Webpage using JavaScript?</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/how-to-redirect-to-another-webpage-using-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Python: What Does if __name__ == &#8220;__main__&#8221; Do ?</title>
		<link>https://info-spot.net/python-what-does-if-__name__-__main__-do/</link>
					<comments>https://info-spot.net/python-what-does-if-__name__-__main__-do/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 29 Nov 2024 04:43:39 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[name main]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=605</guid>

					<description><![CDATA[<p>The __name__ variable Before executing code, Python interpreter reads source file and define few special variables/global variables.&#160;If the&#8230;</p>
<p>The post <a href="https://info-spot.net/python-what-does-if-__name__-__main__-do/">Python: What Does if __name__ == &#8220;__main__&#8221; Do ?</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h4 class="wp-block-heading">The __name__ variable</h4>



<p>Before executing code, Python interpreter reads source file and define few special variables/global variables.&nbsp;<br>If the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value&nbsp;<strong>“__main__”</strong>. If this file is being imported from another module, __name__ will be set to the&nbsp;<strong>module’s name.</strong>&nbsp;Module’s name is available as value to __name__ global variable.&nbsp;</p>



<h4 class="wp-block-heading">What is a module</h4>



<p>A module is a file containing Python definitions and statements. The file name is the module name with the extension .py. </p>



<p>This is how we execute a file as command to the python interpreter,  </p>



<pre class="wp-block-code"><code>python script.py</code></pre>



<pre class="wp-block-code"><code>print ("Always executed")

if __name__ == "__main__": 
	print ("Executed when invoked directly")
else: 
	print ("Executed when imported")</code></pre>



<ul class="wp-block-list">
<li>All of the code that is at indentation level 0 [Block 1] gets executed. Functions and classes are defined but their code is not executed.</li>



<li>Here, as we executed script.py directly __name__ variable will be<strong> __main__</strong>. So, code in this if block[Block 2] will only run if that module is the entry point to your program. </li>



<li>Thus, you can test whether your script is being run directly or being imported by something else by testing __name__ variable.</li>



<li>If script is getting imported by some other module at that time <strong>__name__ </strong>will be module name.</li>
</ul>



<h4 class="wp-block-heading"><strong>Why and How to use it?</strong></h4>



<p>For example we are developing script which is designed to be used as module. Now if we want to use that module by importing we have to comment out our call. Rather than that approach, it is better to use below code: </p>



<pre class="wp-block-code"><code>if __name__ == "__main__":
    my_function()
 
import myscript
 
myscript.my_function()</code></pre>



<h4 class="wp-block-heading"><strong>Benefits: </strong></h4>



<ol class="wp-block-list">
<li>Python files can act as either reusable modules, or as standalone programs.</li>



<li>if __name__ == “__main__”: is used to execute some code <strong>only</strong> if the file was run directly, and not imported.</li>



<li>If you import this script as a module in another script, the __name__ is set to the name of the script/module.</li>
</ol>



<p></p>



<p></p>



<p></p>



<p></p>
<p>The post <a href="https://info-spot.net/python-what-does-if-__name__-__main__-do/">Python: What Does if __name__ == &#8220;__main__&#8221; Do ?</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/python-what-does-if-__name__-__main__-do/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
