blob: c9acca6ce17022a083cea983686d537450bb146a [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Generated by Apache Maven Doxia at 2018-11-03 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Log4j_Kotlin &#x2013; - Apache Log4j Kotlin API Parent</title>
<link rel="stylesheet" href="./css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="./css/site.css" type="text/css" />
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script type="text/javascript" src="./js/prettify.min.js"></script>
<script type="text/javascript" src="./js/site.js"></script>
<meta name="Date-Revision-yyyymmdd" content="20181103" />
<meta http-equiv="Content-Language" content="en" />
</head>
<body class="composite">
<a href="http://www.apache.org/events/current-event.html">
<img class=logo-left src="http://www.apache.org/events/current-event-234x60.png"/>
</a>
<img class="logo-right" src="./images/logo.png" alt="Apache log4j logo" />
<a href="https://logging.apache.org/">
<img class="logo-center" src="./images/ls-logo.jpg" alt="Apache logging services logo" />
</a>
<div class="clear"></div>
<div class="navbar">
<div class="navbar-inner">
<div class="container-fluid">
<a class="brand" href="https://logging.apache.org/log4j/kotlin/">Apache Log4j Kotlin API Parent &trade;</a>
<ul class="nav">
<li>
<a href="https://cwiki.apache.org/confluence/display/LOGGING/Home" class="external" target="_blank" title="Logging Wiki">Logging Wiki</a>
</li>
<li>
<a href="https://www.apache.org/" class="external" target="_blank" title="Apache">Apache</a>
</li>
<li>
<a href="../../" title="Logging Services">Logging Services</a>
</li>
<li>
<a href="https://github.com/apache/logging-log4j-kotlin" class="external" target="_blank" title="GitHub">GitHub</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container-fluid">
<table class="layout-table">
<tr>
<td class="sidebar">
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-home"></i>Apache Log4j Kotlin</li>
<li class="none active">
<a href="index.html" title="About">About</a>
</li>
<li class="none">
<a href="download.html" title="Download">Download</a>
</li>
<li class="none">
<a href="usage.html" title="Usage">Usage</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-pencil"></i>For Contributors</li>
<li class="none">
<a href="build.html" title="Building Log4j Kotlin from Source">Building Log4j Kotlin from Source</a>
</li>
<li class="none">
<a href="contributing.html" title="Contributing">Contributing</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-cog"></i>Component Reports</li>
<li class="none">
<a href="log4j-api-kotlin/index.html" title="Log4j Kotlin API">Log4j Kotlin API</a>
</li>
<li class="none">
<a href="log4j-api-kotlin-sample/index.html" title="Log4j Kotlin Samples">Log4j Kotlin Samples</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-info-sign"></i>Project Information</li>
<li class="none">
<a href="dependency-convergence.html" title="Dependency Convergence">Dependency Convergence</a>
</li>
<li class="none">
<a href="dependency-management.html" title="Dependency Management">Dependency Management</a>
</li>
<li class="none">
<a href="team-list.html" title="Project Team">Project Team</a>
</li>
<li class="none">
<a href="mail-lists.html" title="Mailing Lists">Mailing Lists</a>
</li>
<li class="none">
<a href="issue-tracking.html" title="Issue Tracking">Issue Tracking</a>
</li>
<li class="none">
<a href="license.html" title="Project License">Project License</a>
</li>
<li class="none">
<a href="source-repository.html" title="Source Repository">Source Repository</a>
</li>
<li class="none">
<a href="project-summary.html" title="Project Summary">Project Summary</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-cog"></i>Project Reports</li>
<li class="none">
<a href="changes-report.html" title="Changes Report">Changes Report</a>
</li>
<li class="none">
<a href="jira-report.html" title="JIRA Report">JIRA Report</a>
</li>
<li class="none">
<a href="rat-report.html" title="RAT Report">RAT Report</a>
</li>
</ul>
</div>
<div id="poweredBy">
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
<img class="poweredBy" alt="Built by Maven" src="./images/maven-feather.png" />
</a>
</div>
</td>
<td class="content">
<h1>Apache Log4j Kotlin API</h1>
<div id="toc" class="toc">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#usage">Usage</a>
<ul class="sectlevel2">
<li><a href="#api_documentation">API Documentation</a></li>
<li><a href="#configuration">Configuration</a></li>
<li><a href="#substituting_parameters">Substituting Parameters</a></li>
<li><a href="#logger_names">Logger Names</a></li>
</ul>
</li>
<li><a href="#building_from_source">Building From Source</a></li>
<li><a href="#contributing">Contributing</a>
<ul class="sectlevel2">
<li><a href="#getting_started">Getting Started</a></li>
<li><a href="#making_changes">Making Changes</a></li>
<li><a href="#making_trivial_changes">Making Trivial Changes</a></li>
<li><a href="#submitting_changes">Submitting Changes</a></li>
<li><a href="#additional_resources">Additional Resources</a></li>
</ul>
</li>
</ul>
</div>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Log4j Kotlin API is a <a href="https://kotlinlang.org/">Kotlin</a> logging facade based on <a href="https://logging.apache.org/log4j/2.x/">Log4j 2</a>.</p>
</div>
<div class="paragraph">
<p>Log4j Kotlin API uses Log4j 2.x as its logging backend.</p>
</div>
<div class="paragraph">
<p>Log4j Kotlin API uses Log4j 2.x as its logging backend by default, but this can also be replaced with compatible libraries (e.g., <a href="https://logback.qos.ch/">Logback</a>).</p>
</div>
<div class="paragraph">
<p>While this library is not required to use Log4j API in Kotlin, it does provide idiomatic Kotlin APIs which are friendlier to use in Kotlin programs than the Java APIs.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="usage">Usage</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Using the Kotlin API is as simple as mixing in the <a href="https://github.com/apache/logging-log4j-kotlin/blob/master/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/Logging.kt"><code>Logging</code></a> interface to your class. Example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-kotlin" data-lang="kotlin">import org.apache.logging.log4j.kotlin.Logging
class MyClass: BaseClass, Logging {
fun doStuff() {
logger.info("Doing stuff")
}
fun doStuffWithUser(user: User) {
logger.info { "Doing stuff with ${user.name}." }
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>Logging</code> interface can also be mixed into <code>object</code> declarations, including companions. This is generally preferable over the previous approach as there is a single logger created for every instance of the class.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-kotlin" data-lang="kotlin">import org.apache.logging.log4j.kotlin.Logging
class MyClass: BaseClass {
companion object : Logging
...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Alternatively, a more traditional style can be used to instantiate a logger instance:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-kotlin" data-lang="kotlin">import org.apache.logging.log4j.kotlin
class MyClass: BaseClass {
val logger = logger()
...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The function <code>logger()</code> is an extension function on the <code>Any</code> type (or more specifically, any type <code>T</code> that extends <code>Any</code>).</p>
</div>
<div class="sect2">
<h3 id="api_documentation">API Documentation</h3>
<div class="paragraph">
<p>See <a href="https://logging.apache.org/TODO">KDocs</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="configuration">Configuration</h3>
<div class="paragraph">
<p>Log4j Kotlin API uses <a href="https://logging.apache.org/log4j/2.x/manual/configuration.html">Log4j configuration</a> by default.</p>
</div>
<div class="paragraph">
<p>This supports XML, properties files, and Java-based builders, as well as JSON and YAML with additional dependencies.</p>
</div>
</div>
<div class="sect2">
<h3 id="substituting_parameters">Substituting Parameters</h3>
<div class="paragraph">
<p>Unlike Java, Kotlin provides native functionality for <a href="https://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates">string templates</a>.</p>
</div>
<div class="paragraph">
<p>However, using a string template still incurs the message construction cost if the logger level is not enabled. To avoid this, prefer passing a lambda which won&#8217;t be evaluated until necessary.</p>
</div>
<div class="paragraph">
<p>For example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-kotlin" data-lang="kotlin">logger.debug { "Logging in user ${user.name} with birthday ${user.calcBirthday()}" }</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="logger_names">Logger Names</h3>
<div class="paragraph">
<p>Most logging implementations use a hierarchical scheme for matching logger names with logging configuration.</p>
</div>
<div class="paragraph">
<p>In this scheme the logger name hierarchy is represented by '.' characters in the logger name, in a fashion very similar to the hierarchy used for Java/Kotlin package names.</p>
</div>
<div class="paragraph">
<p>The <code>Logger</code> property added by the <code>Logging</code> interface follows this convention: the interface ensures the <code>Logger</code> is automatically named according to the class it is being used in.</p>
</div>
<div class="paragraph">
<p>The value returned when calling the <code>logger()</code> extension method depends on the receiver of the extension. When called within an Object as shown above, the receiver is <code>this</code> and therefore the logger will again be named according to the class it is being used in. However, a logger named via another class can be obtained as well:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-kotlin" data-lang="kotlin">import org.apache.logging.log4j.kotlin
class MyClass: BaseClass {
val logger = SomeOtherClass.logger()
...
}</code></pre>
</div>
</div>
<div class="sect3">
<h4 id="explicitly_named_loggers">Explicitly Named Loggers</h4>
<div class="paragraph">
<p>An explicitly-named logger may be obtained via the <code>logger</code> function that takes a <code>name</code> parameter:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-kotlin" data-lang="kotlin">import org.apache.logging.log4j.kotlin
class MyClass: BaseClass {
val logger = logger("MyCustomLoggerName")
...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This is also needed in scopes that do not have a <code>this</code> Object, such as top-level functions.</p>
</div>
<div class="paragraph">
<p>Unresolved directive in &lt;stdin&gt; - include::download.adoc[]</p>
</div>
<div class="paragraph">
<p>Unresolved directive in &lt;stdin&gt; - include::changes.adoc[]</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="building_from_source">Building From Source</h2>
<div class="sectionbody">
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
The following information is for developers who wish to modify Log4j Kotlin API or contribute.
If your goal is to add logging to your application, then you can use the <a href="build.html">pre-built binaries</a> instead.
</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="title">Building</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">mvn package</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="contributing">Contributing</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You have found a bug or you have an idea for a cool new feature?
Contributing code is a great way to give something back to the open source community.
Before you dig right into the code there are a few guidelines that we need contributors to follow so that we can have a chance of keeping on top of things.</p>
</div>
<div class="sect2">
<h3 id="getting_started">Getting Started</h3>
<div class="ulist">
<ul>
<li>
<p>Make sure you have a <a href="https://issues.apache.org/jira/">Jira account</a>.</p>
</li>
<li>
<p>Make sure you have a <a href="https://github.com/signup/free">GitHub account</a>.</p>
</li>
<li>
<p>If you&#8217;re planning to implement a new feature it makes sense to discuss your changes on the <a href="https://logging.apache.org/log4j/2.x/mail-lists.html">dev list</a> first.
This way you can make sure you&#8217;re not wasting your time on something that isn&#8217;t considered to be in Apache Log4j&#8217;s scope.</p>
</li>
<li>
<p>Submit a ticket for your issue, assuming one does not already exist.</p>
</li>
<li>
<p>Clearly describe the issue including steps to reproduce when it is a bug.</p>
</li>
<li>
<p>Make sure you fill in the earliest version that you know has the issue.</p>
</li>
<li>
<p>Fork the repository on GitHub.</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="making_changes">Making Changes</h3>
<div class="ulist">
<ul>
<li>
<p>Create a topic branch from where you want to base your work (this is usually the master branch).</p>
</li>
<li>
<p>Make commits of logical units.</p>
</li>
<li>
<p>Respect the original code style:</p>
</li>
<li>
<p>Only use spaces for indentation.</p>
</li>
<li>
<p>Create minimal diffs - disable on save actions like reformat source code or organize imports.
If you feel the source code should be reformatted create a separate PR for this change.</p>
</li>
<li>
<p>Check for unnecessary whitespace with <code>git diff --check</code> before committing.</p>
</li>
<li>
<p>Make sure your commit messages are in the proper format.
Your commit message should contain the key of the Jira issue.</p>
</li>
<li>
<p>Make sure you have added the necessary tests for your changes.</p>
</li>
<li>
<p>Run all the tests with <code>sbt "+ test"</code> to assure nothing else was accidentally broken.</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="making_trivial_changes">Making Trivial Changes</h3>
<div class="paragraph">
<p>For changes of a trivial nature to comments and documentation, it is not always necessary to create a new ticket in JIRA.
In this case, it is appropriate to start the first line of a commit with '(doc)' instead of a ticket number.</p>
</div>
</div>
<div class="sect2">
<h3 id="submitting_changes">Submitting Changes</h3>
<div class="ulist">
<ul>
<li>
<p>Sign the <a href="https://www.apache.org/licenses/#clas">Contributor License Agreement</a> if you haven&#8217;t already.</p>
</li>
<li>
<p>Push your changes to a topic branch in your fork of the repository.</p>
</li>
<li>
<p>Submit a pull request to the repository in the apache organization.</p>
</li>
<li>
<p>Update your Jira ticket and include a link to the pull request in the ticket.</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="additional_resources">Additional Resources</h3>
<div class="ulist">
<ul>
<li>
<p><a href="https://issues.apache.org/jira/browse/LOG4J2">Apache Log4j 2 Jira project page</a></p>
</li>
<li>
<p><a href="https://www.apache.org/licenses/#clas">Contributor License Agreement</a></p>
</li>
<li>
<p><a href="https://help.github.com/">General GitHub documentation</a></p>
</li>
<li>
<p><a href="https://help.github.com/send-pull-requests/">GitHub pull request documentation</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
</td>
</tr>
</table>
</div>
<div class="footer">
<p>Copyright &copy; 1999-2018 <a class="external" href="https://www.apache.org/">The Apache Software Foundation</a>. All Rights Reserved.</p>
<p>Apache Logging, Apache Log4j, Log4j, Apache Log4j Audit, Log4j Audit, Apache, the Apache feather logo, and the Apache Logging project logo are trademarks of The Apache Software Foundation.</p>
<p>Site powered by <a class="external" href="http://getbootstrap.com/">Twitter Bootstrap</a>. Icons from <a class="external" href="http://glyphicons.com/">Glyphicons Free</a>.</p>
</div>
</div>
</body>
</html>