Compare commits
14 Commits
master
...
rule/S1312
Author | SHA1 | Date | |
---|---|---|---|
![]() |
6612bf124d | ||
![]() |
b5cdf8c3e3 | ||
![]() |
704b46ff86 | ||
![]() |
644d73ed16 | ||
![]() |
3afe7c4b77 | ||
![]() |
7a4b34cebc | ||
![]() |
7724aa888d | ||
![]() |
262903cd6d | ||
![]() |
eca917f94f | ||
![]() |
1c7d4f739c | ||
![]() |
44cb8a6434 | ||
![]() |
95e52a6176 | ||
![]() |
3b318d3b25 | ||
![]() |
aed4db2f9a |
@ -1,3 +0,0 @@
|
||||
=== on 22 Nov 2013, 12:53:31 Freddy Mallet wrote:
|
||||
Is implemented by \http://jira.codehaus.org/browse/SONARPLUGINS-3280 for Flex
|
||||
|
4
rules/S1312/csharp/compliant.adoc
Normal file
4
rules/S1312/csharp/compliant.adoc
Normal file
@ -0,0 +1,4 @@
|
||||
[source,csharp,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
private static readonly Logger logger;
|
||||
----
|
4
rules/S1312/csharp/metadata.json
Normal file
4
rules/S1312/csharp/metadata.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"title": "Logger fields should be \"private static readonly\"",
|
||||
"quickfix": "targeted"
|
||||
}
|
4
rules/S1312/csharp/noncompliant.adoc
Normal file
4
rules/S1312/csharp/noncompliant.adoc
Normal file
@ -0,0 +1,4 @@
|
||||
[source,csharp,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
public Logger logger;
|
||||
----
|
3
rules/S1312/csharp/resources.adoc
Normal file
3
rules/S1312/csharp/resources.adoc
Normal file
@ -0,0 +1,3 @@
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers[Access modifiers]
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members[`static` class members]
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly[`readonly` keyword]
|
6
rules/S1312/csharp/rule.adoc
Normal file
6
rules/S1312/csharp/rule.adoc
Normal file
@ -0,0 +1,6 @@
|
||||
:language: csharp
|
||||
:private: private
|
||||
:static: static
|
||||
:readonly: readonly
|
||||
|
||||
include::../rule-dotnet.adoc[]
|
@ -22,20 +22,4 @@ public const logger:ILogger = LogUtil.getLogger(MyClass);
|
||||
private static const LOG:ILogger = LogUtil.getLogger(MyClass);
|
||||
----
|
||||
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
|
||||
include::../message.adoc[]
|
||||
|
||||
include::../parameters.adoc[]
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
include::../comments-and-links.adoc[]
|
||||
|
||||
endif::env-github,rspecator-view[]
|
||||
include::../rspecator.adoc[]
|
@ -27,20 +27,4 @@ private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);
|
||||
|
||||
Variables of type ``++org.apache.maven.plugin.logging.Log++`` are ignored.
|
||||
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
|
||||
include::../message.adoc[]
|
||||
|
||||
include::../parameters.adoc[]
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
include::../comments-and-links.adoc[]
|
||||
|
||||
endif::env-github,rspecator-view[]
|
||||
include::../rspecator.adoc[]
|
@ -1,8 +0,0 @@
|
||||
=== Message
|
||||
|
||||
Make the logger "XXXX" private static [final|const] and rename it to comply with the format "{}".
|
||||
|
||||
Make the logger "XXXX" private static [final|const].
|
||||
|
||||
Rename the "XXX" logger to comply with the format "{}".
|
||||
|
@ -13,7 +13,8 @@
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"convention"
|
||||
"convention",
|
||||
"logging"
|
||||
],
|
||||
"extra": {
|
||||
"replacementRules": [
|
||||
|
@ -1,12 +0,0 @@
|
||||
=== Parameters
|
||||
|
||||
.format
|
||||
****
|
||||
_String_
|
||||
|
||||
----
|
||||
LOG(?:GER)?
|
||||
----
|
||||
|
||||
Regular expression used to check the logger names against.
|
||||
****
|
17
rules/S1312/rspecator-dotnet.adoc
Normal file
17
rules/S1312/rspecator-dotnet.adoc
Normal file
@ -0,0 +1,17 @@
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
|
||||
=== Message
|
||||
|
||||
Make the logger "XXXX" {private} {static} {readonly}.
|
||||
|
||||
=== Parameters
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
endif::env-github,rspecator-view[]
|
35
rules/S1312/rspecator.adoc
Normal file
35
rules/S1312/rspecator.adoc
Normal file
@ -0,0 +1,35 @@
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
|
||||
=== Message
|
||||
|
||||
Make the logger "XXXX" private static [final|const] and rename it to comply with the format "{}".
|
||||
|
||||
Make the logger "XXXX" private static [final|const].
|
||||
|
||||
Rename the "XXX" logger to comply with the format "{}".
|
||||
|
||||
=== Parameters
|
||||
|
||||
.format
|
||||
****
|
||||
_String_
|
||||
|
||||
----
|
||||
LOG(?:GER)?
|
||||
----
|
||||
|
||||
Regular expression used to check the logger names against.
|
||||
****
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
=== on 22 Nov 2013, 12:53:31 Freddy Mallet wrote:
|
||||
Is implemented by \http://jira.codehaus.org/browse/SONARPLUGINS-3280 for Flex
|
||||
|
||||
endif::env-github,rspecator-view[]
|
40
rules/S1312/rule-dotnet.adoc
Normal file
40
rules/S1312/rule-dotnet.adoc
Normal file
@ -0,0 +1,40 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Regardless of the logging framework in use (Microsoft.Extension.Logging, Serilog, Log4net, NLog, ...), logger fields should be:
|
||||
|
||||
* *{private}*: this restricts access to the logger from outside the enclosing type (class, struct, record...). Using any other access modifier would allow other types to use the logger to log messages in the type where it's defined.
|
||||
* *{static}*: making the logger field `{static}` will ensure that the lifetime of the object doesn't depend on the lifetime of the instance of the enclosing type.
|
||||
* *{readonly}*: marking the field as `{readonly}` will prevent modifications to the reference of the logger. This ensures that the reference to the logger remains consistent and doesn't get accidentally reassigned during the lifetime of the enclosing type.
|
||||
|
||||
This rule should be activated when https://en.wikipedia.org/wiki/Service_locator_pattern[Service Locator Design pattern] is followed in place of https://en.wikipedia.org/wiki/Dependency_injection[Dependency Injection] for logging.
|
||||
|
||||
The rule supports the most popular logging frameworks:
|
||||
|
||||
* https://www.nuget.org/packages/Microsoft.Extensions.Logging[Microsoft.Extensions.Logging]
|
||||
* https://www.nuget.org/packages/Serilog[Serilog]
|
||||
* https://www.nuget.org/packages/Castle.Core[Castle.Core]
|
||||
* https://www.nuget.org/packages/NLog[NLog]
|
||||
* https://www.nuget.org/packages/log4net[log4net]
|
||||
|
||||
=== Noncompliant code example
|
||||
|
||||
include::{language}/noncompliant.adoc[]
|
||||
|
||||
=== Compliant solution
|
||||
|
||||
include::{language}/compliant.adoc[]
|
||||
|
||||
== Resources
|
||||
|
||||
=== Documentation
|
||||
|
||||
include::{language}/resources.adoc[]
|
||||
|
||||
* Wikipedia - https://en.wikipedia.org/wiki/Service_locator_pattern[Service locator pattern]
|
||||
* Wikipedia - https://en.wikipedia.org/wiki/Dependency_injection[Dependency injection]
|
||||
|
||||
=== Articles & blog posts
|
||||
|
||||
* https://stackoverflow.com/questions/968132/c-sharp-private-static-and-readonly[C# `private`, `static`, and `readonly`]
|
||||
|
||||
include::rspecator-dotnet.adoc[]
|
4
rules/S1312/vbnet/compliant.adoc
Normal file
4
rules/S1312/vbnet/compliant.adoc
Normal file
@ -0,0 +1,4 @@
|
||||
[source,vbnet,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
Private Shared ReadOnly logger As Logger
|
||||
----
|
5
rules/S1312/vbnet/metadata.json
Normal file
5
rules/S1312/vbnet/metadata.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"title": "Logger fields should be \"Private Shared ReadOnly\"",
|
||||
"quickfix": "targeted"
|
||||
}
|
||||
|
4
rules/S1312/vbnet/noncompliant.adoc
Normal file
4
rules/S1312/vbnet/noncompliant.adoc
Normal file
@ -0,0 +1,4 @@
|
||||
[source,vbnet,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
Public logger As Logger
|
||||
----
|
3
rules/S1312/vbnet/resources.adoc
Normal file
3
rules/S1312/vbnet/resources.adoc
Normal file
@ -0,0 +1,3 @@
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/access-levels[Access levels]
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/shared[`Shared` class members]
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/readonly[`ReadOnly`]
|
6
rules/S1312/vbnet/rule.adoc
Normal file
6
rules/S1312/vbnet/rule.adoc
Normal file
@ -0,0 +1,6 @@
|
||||
:language: vbnet
|
||||
:private: Private
|
||||
:static: Shared
|
||||
:readonly: ReadOnly
|
||||
|
||||
include::../rule-dotnet.adoc[]
|
Loading…
x
Reference in New Issue
Block a user