Skip to content

Commit

Permalink
Warn about each instance of a mis-configured appender (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
swebb2066 authored May 16, 2024
1 parent 9ed183a commit b879b2b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
31 changes: 31 additions & 0 deletions src/main/cpp/appenderskeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,37 @@ void AppenderSkeleton::doAppendImpl(const spi::LoggingEventPtr& event, Pool& poo
append(event, pool1);
}

bool AppenderSkeleton::AppenderSkeletonPrivate::checkNotClosed()
{
if (this->closed)
{
if (!this->warnedClosed)
{
LogLog::warn(LOG4CXX_STR("Not allowed to write to a closed appender."));
this->warnedClosed = true;
}
return false;
}
return true;
}

bool AppenderSkeleton::AppenderSkeletonPrivate::checkLayout()
{
if (!this->layout)
{
if (!this->warnedNoLayout)
{
this->errorHandler->error
( LogString(LOG4CXX_STR("No layout set for the appender named ["))
+ this->name + LOG4CXX_STR("].")
);
this->warnedNoLayout = true;
}
return false;
}
return true;
}

void AppenderSkeleton::setErrorHandler(const spi::ErrorHandlerPtr errorHandler1)
{
std::lock_guard<std::recursive_mutex> lock(m_priv->mutex);
Expand Down
42 changes: 9 additions & 33 deletions src/main/cpp/writerappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,46 +107,22 @@ void WriterAppender::append(const spi::LoggingEventPtr& event, Pool& pool1)
value <code>false</code> is returned. */
bool WriterAppender::checkEntryConditions() const
{
static bool warnedClosed = false;
static bool warnedNoWriter = false;
static bool warnedNoLayout = false;

if (_priv->closed)
{
if (!warnedClosed)
{
LogLog::warn(LOG4CXX_STR("Not allowed to write to a closed appender."));
warnedClosed = true;
}

return false;
}
return _priv->checkWriter() && _priv->checkLayout() && _priv->checkNotClosed();
}

if (_priv->writer == 0)
bool WriterAppender::WriterAppenderPriv::checkWriter()
{
if (this->writer == 0)
{
if (!warnedNoWriter)
if (!this->warnedNoWriter)
{
_priv->errorHandler->error(
this->errorHandler->error(
LogString(LOG4CXX_STR("No output stream or file set for the appender named [")) +
_priv->name + LOG4CXX_STR("]."));
warnedNoWriter = true;
}

return false;
}

if (_priv->layout == 0)
{
if (!warnedNoLayout)
{
_priv->errorHandler->error(
LogString(LOG4CXX_STR("No layout set for the appender named [")) +
_priv->name + LOG4CXX_STR("]."));
warnedNoLayout = true;
this->name + LOG4CXX_STR("]."));
this->warnedNoWriter = true;
}
return false;
}

return true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/include/log4cxx/private/appenderskeleton_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ struct AppenderSkeleton::AppenderSkeletonPrivate

LOG4CXX_NS::helpers::Pool pool;
mutable std::recursive_mutex mutex;

bool warnedClosed = false;
bool checkNotClosed();

bool warnedNoLayout = false;
bool checkLayout();
};

}
Expand Down
3 changes: 3 additions & 0 deletions src/main/include/log4cxx/private/writerappender_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ struct WriterAppender::WriterAppenderPriv : public AppenderSkeleton::AppenderSke
#if LOG4CXX_EVENTS_AT_EXIT
helpers::AtExitRegistry::Raii atExitRegistryRaii;
#endif

bool warnedNoWriter = false;
bool checkWriter();
};

}
Expand Down

0 comments on commit b879b2b

Please sign in to comment.