There is a view that Solomon wrote (and collected) the proverbs when he was young, the Song of Songs before mid-life, and Ecclesiastes when he was old.
12:13-14 The end of the matter; all has been heard. Fear God, and keep his commandments; for that is the whole duty of everyone. For God will bring every deed into judgement, including every secret thing, whether good or evil.
It brings a sadness to the whole book: here’s an old man, a king, who’s been rebuked by God for his sins. He knows where he went wrong, but he’s now unable or unwilling to do anything about it.