Skip to content

Commit

Permalink
Replace static loggers with instance-based (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Aug 1, 2021
1 parent 0072db3 commit f376d69
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 88 deletions.
1 change: 1 addition & 0 deletions Quartz.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Dockerfile = Dockerfile
README.md = README.md
package.json = package.json
docker-compose.yml = docker-compose.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Quartz", "src\Quartz\Quartz.csproj", "{0545C4AC-45BE-47C3-81B7-1592579B9C31}"
Expand Down
2 changes: 1 addition & 1 deletion docker/sqlserver-mot/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM microsoft/mssql-server-linux:2017-latest
FROM mcr.microsoft.com/mssql/server:2017-latest

ENV SA_PASSWORD Quartz!DockerP4ss
ENV ACCEPT_EULA Y
Expand Down
2 changes: 1 addition & 1 deletion docker/sqlserver/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM microsoft/mssql-server-linux:2017-latest
FROM mcr.microsoft.com/mssql/server:2017-latest

ENV SA_PASSWORD Quartz!DockerP4ss
ENV ACCEPT_EULA Y
Expand Down
7 changes: 6 additions & 1 deletion src/Quartz.Jobs/Job/SendMailJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Quartz.Job
/// <author>Marko Lahma (.NET)</author>
public class SendMailJob : IJob
{
private static readonly ILog log = LogProvider.GetLogger(typeof(SendMailJob));
private readonly ILog log;

/// <summary> The host name of the smtp server. REQUIRED.</summary>
public const string PropertySmtpHost = "smtp_host";
Expand Down Expand Up @@ -73,6 +73,11 @@ public class SendMailJob : IJob
/// <summary> The message subject and body content type. Optional.</summary>
public const string PropertyEncoding = "encoding";

public SendMailJob()
{
log = LogProvider.GetLogger(typeof(SendMailJob));
}

/// <summary>
/// Executes the job.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Quartz.Plugins/Plugin/Management/ShutdownHookPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ namespace Quartz.Plugin.Management
/// <author>Marko Lahma (.NET)</author>
public class ShutdownHookPlugin : ISchedulerPlugin
{
private static readonly ILog log = LogProvider.GetLogger(typeof(ShutdownHookPlugin));
private readonly ILog log;

public ShutdownHookPlugin()
{
log = LogProvider.GetLogger(typeof(ShutdownHookPlugin));
CleanShutdown = true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Quartz/Impl/AdoJobStore/AdoUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ namespace Quartz.Impl.AdoJobStore
/// <author>Marko Lahma</author>
public class AdoUtil
{
private static readonly ILog log = LogProvider.GetLogger("Quartz.SQL");
private readonly ILog log;
private readonly IDbProvider dbProvider;

public AdoUtil(IDbProvider dbProvider)
{
this.dbProvider = dbProvider;
log = LogProvider.GetLogger("Quartz.SQL");
}

public void AddCommandParameter(IDbCommand cmd, string paramName, object? paramValue)
Expand Down
3 changes: 2 additions & 1 deletion src/Quartz/Impl/AdoJobStore/ClusterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Quartz.Impl.AdoJobStore
{
internal class ClusterManager
{
private static readonly ILog log = LogProvider.GetLogger(typeof(ClusterManager));
private readonly ILog log;

// keep constant lock requestor id for manager's lifetime
private readonly Guid requestorId = Guid.NewGuid();
Expand All @@ -26,6 +26,7 @@ internal ClusterManager(JobStoreSupport jobStoreSupport)
{
this.jobStoreSupport = jobStoreSupport;
cancellationTokenSource = new CancellationTokenSource();
log = LogProvider.GetLogger(typeof(ClusterManager));
}

public async Task Initialize()
Expand Down
5 changes: 3 additions & 2 deletions src/Quartz/Impl/AdoJobStore/ConnectionAndTransactionHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ namespace Quartz.Impl.AdoJobStore
/// <author>Marko Lahma</author>
public class ConnectionAndTransactionHolder : IDisposable
{
private static readonly ILog log = LogProvider.GetLogger(typeof(ConnectionAndTransactionHolder));

private DateTimeOffset? sigChangeForTxCompletion;

private readonly DbConnection connection;
Expand Down Expand Up @@ -91,6 +89,8 @@ public void Close()
}
catch (Exception e)
{
var log = LogProvider.GetLogger(typeof(ConnectionAndTransactionHolder));

log.ErrorException(
"Unexpected exception closing Connection." +
" This is often due to a Connection being returned after or during shutdown.", e);
Expand Down Expand Up @@ -148,6 +148,7 @@ public void Rollback(bool transientError)
}
catch (Exception e)
{
var log = LogProvider.GetLogger(typeof(ConnectionAndTransactionHolder));
if (transientError)
{
// original error was transient, ones we have in Azure, don't complain too much about it
Expand Down
3 changes: 2 additions & 1 deletion src/Quartz/Impl/AdoJobStore/MisfireHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Quartz.Impl.AdoJobStore
{
internal class MisfireHandler
{
private static readonly ILog log = LogProvider.GetLogger(typeof (MisfireHandler));
private readonly ILog log;
// keep constant lock requestor id for handler's lifetime
private readonly Guid requestorId = Guid.NewGuid();

Expand All @@ -23,6 +23,7 @@ internal class MisfireHandler
internal MisfireHandler(JobStoreSupport jobStoreSupport)
{
this.jobStoreSupport = jobStoreSupport;
log = LogProvider.GetLogger(typeof(MisfireHandler));

string threadName = $"QuartzScheduler_{jobStoreSupport.InstanceName}-{jobStoreSupport.InstanceId}_MisfireHandler";
taskScheduler = new QueuedTaskScheduler(threadCount: 1, threadName: threadName, useForegroundThreads: !jobStoreSupport.MakeThreadsDaemons);
Expand Down
33 changes: 16 additions & 17 deletions src/Quartz/Impl/SchedulerDetailsSetter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#region License
/*
/*
* All content copyright Marko Lahma, unless otherwise indicated. All rights reserved.
*
* Licensed 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
*
* Licensed 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.
*
*
*/
#endregion

Expand All @@ -35,8 +35,6 @@ namespace Quartz.Impl
/// <author>Marko Lahma (.NET)</author>
internal static class SchedulerDetailsSetter
{
private static readonly ILog log = LogProvider.GetLogger(typeof(SchedulerDetailsSetter));

internal static void SetDetails(object target, string schedulerName, string schedulerId)
{
Set(target, "InstanceName", schedulerName);
Expand All @@ -45,12 +43,13 @@ internal static void SetDetails(object target, string schedulerName, string sche

private static void Set(object target, string propertyName, string propertyValue)
{
try
try
{
ObjectUtils.SetPropertyValue(target, propertyName, propertyValue);
}
}
catch (MemberAccessException)
{
var log = LogProvider.GetLogger(typeof(SchedulerDetailsSetter));
log.WarnFormat("Unable to set property {0} for {1}. Possibly older binary compilation.", propertyName, target);
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/Quartz/Impl/StdSchedulerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,14 @@ public class StdSchedulerFactory : ISchedulerFactory

private PropertiesParser cfg = null!;

private static readonly ILog log = LogProvider.GetLogger(typeof(StdSchedulerFactory));
private ILog log = null!;

private string SchedulerName
{
// ReSharper disable once ArrangeAccessorOwnerBody
get { return cfg.GetStringProperty(PropertySchedulerInstanceName, "QuartzScheduler")!; }
}

private ILog Log => log;

/// <summary>
/// Returns a handle to the default Scheduler, creating it if it does not
/// yet exist.
Expand Down Expand Up @@ -208,7 +206,8 @@ public virtual void Initialize()
throw initException;
}

var props = InitializeProperties(Log, throwOnProblem: true);
log ??= LogProvider.GetLogger(typeof(StdSchedulerFactory));
var props = InitializeProperties(log, throwOnProblem: true);
Initialize(OverrideWithSysProps(props ?? new NameValueCollection()));
}

Expand Down Expand Up @@ -318,7 +317,7 @@ protected virtual void ValidateConfiguration()
// now check against allowed
foreach (var configurationKey in cfg.UnderlyingProperties.AllKeys)
{
if (configurationKey is null
if (configurationKey is null
|| !configurationKey.StartsWith(ConfigurationKeyPrefix)
|| configurationKey.StartsWith(ConfigurationKeyPrefixServer))
{
Expand Down Expand Up @@ -355,6 +354,8 @@ private async Task<IScheduler> Instantiate()
throw initException;
}

log ??= LogProvider.GetLogger(typeof(StdSchedulerFactory));

ISchedulerExporter? exporter = null;
IJobStore js;
IThreadPool tp;
Expand Down Expand Up @@ -734,7 +735,7 @@ private async Task<IScheduler> Instantiate()
}

jobStoreSupport.LockHandler = lockHandler;
Log.Info("Using custom data access locking (synchronization): " + lockHandlerType);
log.Info("Using custom data access locking (synchronization): " + lockHandlerType);
}
catch (Exception e)
{
Expand Down Expand Up @@ -926,7 +927,7 @@ private async Task<IScheduler> Instantiate()
}
catch (Exception e)
{
Log.ErrorException("Couldn't generate instance Id!", e);
log.ErrorException("Couldn't generate instance Id!", e);
throw new InvalidOperationException("Cannot run without an instance id.");
}
}
Expand Down Expand Up @@ -1009,9 +1010,9 @@ private async Task<IScheduler> Instantiate()
jrsf.Initialize(sched);
qs.Initialize();

Log.Info("Quartz scheduler '{0}' initialized".FormatInvariant(sched.SchedulerName));
log.Info("Quartz scheduler '{0}' initialized".FormatInvariant(sched.SchedulerName));

Log.Info("Quartz scheduler version: {0}".FormatInvariant(qs.Version));
log.Info("Quartz scheduler version: {0}".FormatInvariant(qs.Version));

// prevents the repository from being garbage collected
qs.AddNoGCObject(schedRep);
Expand Down Expand Up @@ -1057,7 +1058,7 @@ private async Task ShutdownFromInstantiateException(IThreadPool? tp, QuartzSched
}
catch (Exception e)
{
Log.ErrorException("Got another exception while shutting down after instantiation exception", e);
log.ErrorException("Got another exception while shutting down after instantiation exception", e);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/Quartz/Impl/Triggers/CronTriggerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ namespace Quartz.Impl.Triggers
[Serializable]
public class CronTriggerImpl : AbstractTrigger, ICronTrigger
{
private static readonly ILog logger = LogProvider.GetLogger(typeof(CronTriggerImpl));

protected const int YearToGiveupSchedulingAt = 2299;
private CronExpression? cronEx;
private DateTimeOffset startTimeUtc = DateTimeOffset.MinValue;
Expand Down Expand Up @@ -623,10 +621,11 @@ public override IScheduleBuilder GetScheduleBuilder()
case Quartz.MisfireInstruction.CronTrigger.FireOnceNow:
cb.WithMisfireHandlingInstructionFireAndProceed();
break;
case Quartz.MisfireInstruction.IgnoreMisfirePolicy:
case Quartz.MisfireInstruction.IgnoreMisfirePolicy:
cb.WithMisfireHandlingInstructionIgnoreMisfires();
break;
default:
var logger = LogProvider.GetLogger(typeof(CronTriggerImpl));
logger.Warn($"Unrecognized misfire policy {MisfireInstruction}. Derived builder will use the default cron trigger behavior (FireOnceNow)");
break;
}
Expand Down
9 changes: 7 additions & 2 deletions src/Quartz/Simpl/PropertySettingJobFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ namespace Quartz.Simpl
/// <author>Marko Lahma (.NET)</author>
public class PropertySettingJobFactory : SimpleJobFactory
{
private static readonly ILog log = LogProvider.GetLogger(typeof(PropertySettingJobFactory));
private readonly ILog log;

/// <summary>
public PropertySettingJobFactory()
{
log = LogProvider.GetLogger(typeof(PropertySettingJobFactory));
}

/// <summary>
/// Whether the JobInstantiation should fail and throw and exception if
/// a key (name) and value (type) found in the JobDataMap does not
/// correspond to a property setter on the Job class.
Expand Down
41 changes: 23 additions & 18 deletions src/Quartz/Simpl/SimpleJobFactory.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#region License
/*
/*
* All content copyright Marko Lahma, unless otherwise indicated. All rights reserved.
*
* Licensed 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
*
* Licensed 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.
*
*
*/
#endregion

Expand All @@ -25,8 +25,8 @@

namespace Quartz.Simpl
{
/// <summary>
/// The default JobFactory used by Quartz - simply calls
/// <summary>
/// The default JobFactory used by Quartz - simply calls
/// <see cref="ObjectUtils.InstantiateType{T}" /> on the job class.
/// </summary>
/// <seealso cref="IJobFactory" />
Expand All @@ -35,9 +35,14 @@ namespace Quartz.Simpl
/// <author>Marko Lahma (.NET)</author>
public class SimpleJobFactory : IJobFactory
{
private static readonly ILog log = LogProvider.GetLogger(typeof (SimpleJobFactory));
private readonly ILog log;

/// <summary>
public SimpleJobFactory()
{
log = LogProvider.GetLogger(typeof(SimpleJobFactory));
}

/// <summary>
/// Called by the scheduler at the time of the trigger firing, in order to
/// produce a <see cref="IJob" /> instance on which to call Execute.
/// </summary>
Expand Down Expand Up @@ -76,7 +81,7 @@ public virtual IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
}

/// <summary>
/// Allows the job factory to destroy/cleanup the job if needed.
/// Allows the job factory to destroy/cleanup the job if needed.
/// No-op when using SimpleJobFactory.
/// </summary>
public virtual void ReturnJob(IJob job)
Expand Down
3 changes: 2 additions & 1 deletion src/Quartz/Simpl/TaskSchedulingThreadPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Quartz.Simpl
/// </summary>
public abstract class TaskSchedulingThreadPool : IThreadPool
{
private static readonly ILog log = LogProvider.GetLogger(typeof(TaskSchedulingThreadPool));
private readonly ILog log;

// The token source used to cancel thread pool execution at shutdown
// Note that cancellation is not propagated to the user-scheduled tasks currently executing,
Expand Down Expand Up @@ -106,6 +106,7 @@ public TaskSchedulingThreadPool() : this(DefaultMaxConcurrency)

public TaskSchedulingThreadPool(int maxConcurrency)
{
log = LogProvider.GetLogger(typeof(TaskSchedulingThreadPool));
MaxConcurrency = maxConcurrency;
}

Expand Down

0 comments on commit f376d69

Please sign in to comment.