With a large volume of queries per second logging all queries gets expensive, it would be nice to be able to log only a configurable fraction of the queries, selected at random.
{
    
    if (auto quota = context->getQuota())
        quota->used(Quota::ERRORS, 1,  false);
    const Settings & settings = context->getSettingsRef();
    
    QueryLogElement elem;
    elem.type = QueryLogElementType::EXCEPTION_BEFORE_START;
    
    
    
    elem.event_time = current_time_us / 1000000;
    elem.event_time_microseconds = current_time_us;
    elem.query_start_time = current_time_us / 1000000;
    elem.query_start_time_microseconds = current_time_us;
    elem.current_database = context->getCurrentDatabase();
    elem.query = query_for_logging;
    elem.normalized_query_hash = normalizedQueryHash<false>(query_for_logging);
    
    if (ast)
    {
        elem.query_kind = ast->getQueryKindString();
        if (settings.log_formatted_queries)
            elem.formatted_query = queryToString(ast);
    }
    
    elem.exception_code = getCurrentExceptionCode();
    elem.exception = getCurrentExceptionMessage(false);